我有以下代码来执行跨域请求并获取 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) )
and 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); ?????
}
});
请记住,服务器必须以回调函数(数据)的形式返回数据,其中数据是 json 对象,就像您在标准 json 调用中返回一样。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)