【问题标题】:Bing Map REST API Json not getting data, tried everything I couldBing Map REST API Json没有获取数据,尽我所能
【发布时间】:2017-06-21 14:17:48
【问题描述】:

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({
	url:url,
	type:"GET",
	dataType:"jason",
	success: function(data){
		lat=data.resourceSets[0].resources[0].point.coordinates[0];
	},
	error: function(r){ console.log(r)}
	});
我得到错误: XMLHttpRequest 无法加载 https://dev.virtualearth.net/REST/v1/Locations?query=Oakleaf%20Dr%20Meadow%...=json&key=... 请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin '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));
  }
});

然后,我得到“Uncaught SyntaxError: Unexpected token :” 我发现最多的答案是对服务器端脚本进行更改,这在这种情况下不适用。现在呢?

【问题讨论】:

  • 语法错误是错误函数抛出的吗?错误:函数(响应){ console.log(JSON.stringify(响应)); } 或在请求期间

标签: json rest dictionary bing-maps bing


【解决方案1】:

好吧,我确定语法错误来自错误函数

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

这将不起作用,除非响应具有 JSON 标头,因此基本上如果此请求到达服务器并且服务器给您一个错误,那么您必须在服务器端向响应添加标头否则你可以用

替换它
console.log(response); 

这样,它会写一个对象到控制台

【讨论】:

    猜你喜欢
    • 2020-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-15
    • 2017-10-30
    • 2023-02-22
    • 2019-02-23
    • 2018-09-03
    相关资源
    最近更新 更多