0
我正在學習AngularJS。 我正在嘗試以JSON響應格式獲取GoogleMapsAPI。 但Chrome報告「Uncaught SyntaxError:Unexpected token:」。GoogleMapsAPI的JSON「Uncaught SyntaxError:意外的令牌:」
到目前爲止,我只是試圖連接,並使用下面的代碼中看到的對象的信息:
var deferred = $q.defer();
$http.jsonp('https://maps.googleapis.com/maps/api/distancematrix/json?origins=Tokyo&destinations=Kyoto&mode=Driving&sensor=false', {
params: {
callback: 'JSON_CALLBACK'
}
}).success(function(response){
console.dir(data,status,headers,config);
deferred.resolve(data);
}).error(function(data,status,headers,config){
deferred.reject(status);
});
return deferred.promise;
的JSON是回來的響應,而Chrome報告「未捕獲的SyntaxError:意外的標記:」 我不斷收到在控制檯以下錯誤:
Uncaught SyntaxError: Unexpected token : (18:20:02:095 | error, javascript)
at https://maps.googleapis.com/maps/api/distancematrix/json?origins=Tokyo&destinations=Kyoto&mode=Driving&sensor=false&callback=angular.callbacks._0:2
GoogleMapsApi的JSON響應:
{
"destination_addresses" : [ "Kyoto, Kyoto Prefecture, Japan" ],
"origin_addresses" : [ "Tokyo, Japan" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "457 km",
"value" : 456931
},
"duration" : {
"text" : "5 hours 39 mins",
"value" : 20338
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
如果我直接導航到url本身,則返回JSON並顯示在瀏覽器中。
任何想法,爲什麼我不能得到這個錯誤,我從來沒有成功的回調?
我不知道JSON和JSONP之間的區別。謝謝!我試過你的建議,這是不可能的。請問您可以檢查此鏈接嗎?https://jsfiddle.net/dt70sm/oa4h999w/1/ –
您正遇到[CORS](http://enable-cors.org)問題。要繞過此操作,您可以使用[Chrome擴展程序](https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi/related?hl=en)禁用同源策略。這會降低瀏覽器的安全性,所以只能根據需要使用擴展名;-) –
Thankyou爲您解答。 Jsfiddle的樣本是通過這個擴展工作的。需要時我會使用這個擴展。謝謝! :d –