Javascript 回调 - 通过 jQuery AJAX 解析错误 JSONP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11786325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Callback - Parseerror JSONP via jQuery AJAX
提问by McKev
Please consider the following code snippet:
请考虑以下代码片段:
$(function () {
$.ajax({
type: "GET",
url: "http://mosaicco.force.com/siteforce/activeretailaccounts",
dataType: "jsonp",
crossDomain: true,
jsonp: "callback",
error: function(jqXHR, textStatus, errorThrown) {
alert('Error Message: '+textStatus);
alert('HTTP Error: '+errorThrown);
},
success: function (data) {
var i = 0;
//Process JSON
$.each(data, function () {
var name = this.locname;
var lat = this.lat;
var lng = this.lng;
var address = this.address;
var address2 = this.address2;
var city = this.city;
var state = this.state;
var postal = this.postal;
var phone = this.phone;
var web = this.web;
web = web.replace("http://", "");
var distance = GeoCodeCalc.CalcDistance(orig_lat, orig_lng, lat, lng, GeoCodeCalc.EarthRadiusInMiles);
//Create the array
locationset[i] = new Array(distance, name, lat, lng, address, address2, city, state, postal, phone, web);
i++;
});
}
});
});?
I am pulling JSON cross domain and for some reason I keep getting a parseerror returned:
我正在拉 JSON 跨域,出于某种原因,我不断收到返回的解析错误:
HTTP Error:Error: jQuery17209875996995251626_1343943259805 was not called
I am able to see the data just fine on the page - and the callback looks like this:
我能够在页面上很好地看到数据 - 回调如下所示:
callback:jQuery17209875996995251626_1343943259805_:1343943260015
Please help me to diagnose what I am missing - thanks!
请帮我诊断我遗漏了什么 - 谢谢!
回答by Bergi
var data = $.parseJSON(data);
Since you are doing a JSONP request, you will get an object literal passed into the callback function. You can't use parseJSON
on that. Anyway, jQuery is intelligent and always does the parse for you if the content-type is known.
由于您正在执行 JSONP 请求,因此您将获得一个传递给回调函数的对象文字。你不能用parseJSON
那个。无论如何,jQuery 是智能的,如果内容类型已知,它总是会为您解析。
Not sure whether that triggers the error message, but to the question jQuery JSON response always triggers a ParseErrorthis was the solution.
不确定这是否会触发错误消息,但对于jQuery JSON response always triggers a ParseError这个问题,这是解决方案。
OK, that's simple. Look at the script it loads: That is no valid JSONP- it misses the callback padding. Also, the mime-type is wrong: For a JSONP script, it should be text/javascript
or application/javascript
, for the JSON they deliver it should be application/json
.
好的,这很简单。查看它加载的脚本: 这不是有效的 JSONP- 它错过了回调填充。此外,mime-type 是错误的:对于 JSONP 脚本,它应该是text/javascript
或application/javascript
,对于它们提供的 JSON,它应该是application/json
。
jQuery does detect the load of the "script", but as nothing gets executed it throws an the error that "the given callback was not called although the file was successfully loaded" - parseerror suspected.
jQuery 确实检测到“脚本”的加载,但是当没有执行任何操作时,它会抛出“尽管文件已成功加载但未调用给定回调”的错误 - 怀疑是解析错误。
Are you sure that the webservice supports JSONP at all?
您确定 Web 服务完全支持 JSONP 吗?