2017-06-21 15 views
1

兵地圖REST API的Json沒有得到數據,試圖盡我所能

var url = "https://dev.virtualearth.net/REST/v1/Locations?query=" 
 
+ encodeURIComponent('Oakleaf Dr Meadow Valley CA 95956 United States') 
 
+ "&incl=queryParse" 
 
+ "&output=json" 
 
+ "&key=" + key 
 
$.ajax({ 
 
\t url:url, 
 
\t type:"GET", 
 
\t dataType:"jason", 
 
\t success: function(data){ 
 
\t \t lat=data.resourceSets[0].resources[0].point.coordinates[0]; 
 
\t }, 
 
\t error: function(r){ console.log(r)} 
 
\t });
我得到錯誤: 的XMLHttpRequest無法加載 https://dev.virtualearth.net/REST/v1/Locations?query=Oakleaf%20Dr%20Meadow% ... = JSON &鍵= ...沒有「訪問控制允許來源」標題出現在請求的資源上。因此不允許原產地'null'訪問。

好的,我搜索了答案,必須使用jsonp。

$.ajax({ 
url: url, 
    type: "GET", 
    dataType: 'jsonp', 
    jsonp: "jsonp", 
    success: function(data) { 
var lat=data.resourceSets[0].resources[0].point.coordinates[0], 
lng=data.resourceSets[0].resources[0].point.coordinates[1]; 
    console.log(lat); 
    }, 
    error: function(response) { 
    console.log(JSON.stringify(response)); 
    } 
}); 

然後,我得到「未捕獲的SyntaxError:意外的標記:」 答案最讓我發現是修改服務器端腳本,這是不是適用於這種情況。怎麼辦?

+0

做錯誤函數的語法錯誤拋出?錯誤:函數(響應){console.log(JSON.stringify(response)); } 或請求期間 –

回答

0

好了,我敢肯定的是語法錯誤是由誤差函數

console.log(JSON.stringify(response)); 

未來這將無法工作,除非如果響應具有JSON頭,所以基本上如果這個請求到達服務器和服務器給你的錯誤,那麼你必須添加一個頭在服務器端的響應,否則你可以通過

console.log(response); 

以這種方式取代它,它會寫一個對象安慰