【发布时间】:2016-04-21 17:25:48
【问题描述】:
这是一个非常简单的带有匿名回调的 AJAX 请求。 complete 和 error 都在开火。为什么?
$.ajax({
url:'/echo/js/?js=hello%20world!',
complete: function (response) {
console.log("In anonymous success callback");
console.log("response text: " + response.responseText);
console.log("response object:");
console.log(response);
},
error: function (error) {
console.log("In anonymous error callback");
console.log("error object:");
console.log(error);
}
});
https://jsfiddle.net/abalter/tz0tu04y/
编辑 我尝试使用承诺,现在我只得到错误。我的“非常简单”的 ajax 一定有问题。
$.ajax({
url: '/echo/js/?js=hello%20world!',
type: 'POST',
})
.done(function(response) {
console.log("In anonymous JS success callback");
console.log("response text: " + response.responseText);
console.log("response object:");
console.log(response);
})
.fail(function(error) {
console.log("In anonymous JS error callback");
console.log("error object:");
console.log(error);
})
.always(function(data) {
console.log("JS I will always do this");
console.log(data);
});
【问题讨论】:
-
改用成功和错误回调。
-
RTFM.
-
好的。我是 RTFM,并按照@escaparello 的建议使用了承诺。仍然出现错误。更新的问题。
-
@abalter 检查我对第二个问题的解决方案的答案
标签: javascript jquery ajax callback anonymous-function