我正在尝试从Google Maps API解析JSON,并且我想出了这个错误。
“XMLHttpRequest无法加载https://maps.googleapis.com/maps/api/distancematrix/json?origins=39.9504628 ... SA& mode = driving& language = en-FR& key = AIzaSyCwFXB-S2ynNz-9jZcGAx26M2gGfsuCcOQ。请求的资源上没有”Access-Control-Allow-Origin“标头因此,不允许原点“http://localhost:8000”访问。“
这是代码:
var jsonLink = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=39.9504628,-75.190088&destinations=Kentucky,+USA&mode=driving&language=en-FR&key=AIzaSyCwFXB-S2ynNz-9jZcGAx26M2gGfsuCcOQ";
$.getJSON(jsonLink, function (json) {
// Set the variables from the results array
var address = json.results[0].rows.elements.distance.text;
console.log('KEK : ', text);
});
// Caching the link jquery object
var $myLink = $('a.myLink');
// Set the links properties
$myLink.prop({
href: jsonLink,
title: 'Click on this link to open in a new window.'
}).click(function (e) {
e.preventDefault();
window.open(this.href, '_blank');
});
})