检索JSON引发错误:意外的令牌:
Retrieving JSON throws error: Unexpected token :
我正在通过AJAX从Google Maps API检索高程数据。
我正在取回我想要的数据,就像我在Chrome中查看控制台一样,我可以看到一个200状态代码,并可以在响应选项卡中看到数据。但它会弹出"Uncaught SyntaxError:Unexpected token:",所以我无法显示JSON文件中的任何内容。
这是我的代码:
var theURL = 'http://maps.googleapis.com/maps/api/elevation/json?locations=' + longitude + ',' + latitude + '&sensor=false&callback=?';
$.ajax({
url: theURL,
type: 'get',
dataType: 'json',
cache: false,
crossDomain: true,
success: function (data) {
var theData = $.parseJSON(data);
console.log(theData);
}
});
实时代码在这里:http://map.colouringcode.com/
我们非常感谢所有的帮助。
Google Maps API不支持直接的JSONP请求。
相反,您需要使用Javascript API。
意识到这个问题已经过时了,我希望这能在未来帮助到一些人。这是我第一次接触javascript,尤其是所有这些JSON内容,因此我在试图弄清楚自己做错了什么时,头都掉了下来。
这是我的解决方案,它检索客户端的位置(在lat和lng中),然后使用谷歌地理编码api以"人类可读"的格式确定它们的位置。
function currentLocation() {
navigator.geolocation.getCurrentPosition(foundLocation, errorLocation);
function foundLocation(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
//This is where the geocoding starts
var locationURL= "http://maps.googleapis.com/maps/api/geocode/json?latlng="
+ lat + "," + lng + "&sensor=false&callback=myLocation"
//This line allows us to get the return object in a JSON
//instead of a JSONP object and therefore solving our problem
$.getJSON(locationURL, myLocation);
}
function errorLocation() {
alert("Error finding your location.");
}
}
function myLocation(locationReturned) {
var town = locationReturned.results[0].address_components[1].short_name;
var state = locationReturned.results[0].address_components[4].short_name;
console.log(town, state);
}
相关文章:
- React JS:未捕获(在承诺中)语法错误:在位置 0 的 JSON 中意外<令牌
- jQuery$.getJSON抛出意外令牌
- 为什么我在下面的..of循环中得到意外令牌
- 组件中的ReactJS意外令牌
- AngularJS $http GET 请求到本地 JSON 文件返回语法错误:意外令牌]
- 巴别塔,意外令牌 (15:33) ::.
- Sencha 2.0 和 Codeigniter RESTful API 生成未捕获的语法错误:意外令牌:
- 在事件上使用 jquery 时出现意外令牌
- JSON 中位置 4 处的意外令牌<
- 意外令牌 ) 在我的代码中
- Electron 和 Babel 6 异步/等待抛出意外令牌
- “JSON.parse(str)”返回“意外令牌c”,但str是一个字符串
- jQuery after() uncatch SyntaxError: 意外令牌 ILLEGAL
- 使用节点javascript的意外令牌ILLEGAL
- Javascript/JQuery-Chrome意外令牌
- Ajax 帖子未捕获语法错误:意外令牌非法
- angularjs-SyntaxError:请求从服务器返回时出现意外令牌
- jquery:使用ember的意外令牌C
- React本机项目中的意外令牌
- 解析有效的 JSON 得到“语法错误:意外令牌非法”