我對所有這些相同來源政策的事情都很瘋狂。當API不允許訪問控制 - 允許來源時怎麼辦
當我嘗試做了谷歌地圖API的請求我沒有問題:
var jsonData = $.ajax({
url:"http://maps.googleapis.com/maps/api/geocode/json?address=",
dataType:"json",
async:true,
success: function(json){...}
}
我認爲,這是因爲谷歌地圖API允許訪問控制允許來源。但是,當我嘗試使用openls.geog.uni-heidelberg.de API我得到的跨域錯誤:
var xmlData = $.ajax({
type: "GET",
url:"http://openls.geog.uni-heidelberg.de/route?"
//dataType:"jsonp xml",
dataType: "xml",
async:true,
crossDomain : true,
success: function(xml){...}
}
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost ' is therefore not allowed access.
人認爲我不明白的是,如果openls API不允許跨-origin爲什麼可以直接從我的瀏覽器做請求剛剛鍵入URL,如:
但我可以使用jQuery無法做到。我也嘗試過jsonp解決方案,但它不適用於xml。
關於發生了什麼的一些想法?
嘗試將'dataType'更改爲'jsonp' - 我想在當前的例子中可以使用JSONP而不是CORS。 – vitozev
@unixarmy鑑於它正在返回XML我不認爲JSONP會起作用。 –
啊,對不起,我沒有注意到。你是對的! :) – vitozev