javascript jQuery ajax 请求错误:未调用回调
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15945113/
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
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:42:05 来源:igfitidea点击:
jQuery ajax request error: callback not called
提问by mike44
window.MyCallback = function (data) {
console.log(data);
};
$(document).ready(function () {
$.ajax({
type: 'GET',
url: 'http://xxx.xxx.xxx.xx/MyService/Service.svc/MyMethod',
contentType: 'application/jsonp',
crossdomain: true,
dataType: "jsonp",
data: { 'username': 'Hyman' },
crossDomain: true,
jsonpCallback: 'MyCallback',
success: function (txt) {
console.log(txt);
},
error: function (xhr, status, err) {
console.log(status, err);
}
});
});
I always get the error: MyCallback was not called.
我总是收到错误: MyCallback was not called.
采纳答案by benjamin54
$.ajax({
type: "GET",
dataType: "jsonp",
contentType: "application/javascript",
data: { 'username': 'Hyman' },
async: false,
url: 'http://xxx.xxx.xxx.xx/MyService/Service.svc/MyMethod',
success: function (jsonData) {
console.log(jsonData);
},
error: function (request, textStatus, errorThrown) {
console.log(request.responseText);
console.log(textStatus);
console.log(errorThrown);
}
});
回答by Sagish
Why not like this?
为什么不喜欢这个?
var mycallback = function (data) {
console.log("callback", data);
};
$(document).ready(function () {
$.ajax({
type: 'GET',
url: 'https://graph.facebook.com/sagish',
contentType: 'application/jsonp',
dataType: "jsonp",
data: { 'username': 'Hyman' },
crossDomain: true,
success: mycallback,
error: function (xhr, status, err) {
console.log(status, err);
}
});
});
回答by benjamin54
For calling the "MyCallback" function, your url should be,
要调用“MyCallback”函数,您的网址应该是,
url: 'http://xxx.xxx.xxx.xx/MyService/Service.svc/MyMethod?callback=?
And server should return the ajax response like
并且服务器应该返回像这样的ajax响应
return "MyCallback(your data here)"