NLV*_*NLV 5 jquery jsonp cross-domain internet-explorer-8
我有以下代码来执行跨域请求并获取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);
}
Run Code Online (Sandbox Code Playgroud)
我将statusText称为'Success',StatusCode为xhr中的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); ?????
}
});
Run Code Online (Sandbox Code Playgroud)
请记住,服务器必须以回调函数(数据)的形式返回数据,其中数据是 json 对象,就像您在标准 json 调用中返回一样。