我有以下代码来执行跨域请求并获取 JSONP 数据(通过回调方法包装的 JSON)。我已经验证我使用包装 JSON 数据的回调方法正确获得了响应。它在 IE7 中工作得很好(回调 cb 被调用),但在 IE8 中则不然。
$(document).ready(function () {
var abc = $.ajax({
type: "GET",
url: "http://sd.domain.com/param1=a¶m2=b&output=json&callback=cb",
dataType: "jsonp",
jsonp: false,
cache: false,
success: function (json) {
},
error: function (e) {
}
});
abc.error(function (data, xhr, dat1) {
});
abc.complete(function (xhr, status) {
var data = xhr.responseText;
});
});
function cb(dd) {
alert(dd.people[0].nameFirst);
}
我在 xhr 中得到的 statusText 为“Success”,StatusCode 为 200。另外,我无法找到任何正确称为 xhr 的responseText。那么如何获得错误/完整函数中的响应呢?有什么想法吗?
请您参考如下方法:
Jquery 自动传递类似于 callback=JQuery132123412415235 的回调,并且服务器必须返回一个使用数据 JQuery132123412415235(data_returned) 调用此函数的脚本,其余的等于标准json请求
您还可以使用 success 和 error 属性,并使用 Promise 和 error(function (data) ) 和 complete(function (data)) 只是为了获得清晰的代码我认为你必须只使用一种方法。代码是这样的:
$(document).ready(function () {
var abc = $.ajax({
type: "GET",
url: "http://sd.domain.com/param1=a¶m2=b&output=json",
dataType: "jsonp",
jsonp: false,
cache: false
});
abc.error(function (data, xhr, dat1) {
});
abc.complete(function (xhr, status) {
var data = xhr.responseText;
});
abc.done(data){
//alert(data.people[0].nameFirst); ?????
}
});
请记住,服务器必须以callback_function(data) 的形式返回数据,其中数据是json 对象,就像您在标准json 调用中返回的那样。
