为什么AJAX完成和错误回调都触发?

aba*_*ter -3 javascript ajax jquery callback anonymous-function

这是一个带有匿名回调的简单的AJAX请求.双方completeerror已触发.为什么?

$.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);
  }
});
Run Code Online (Sandbox Code Playgroud)

https://jsfiddle.net/abalter/tz0tu04y/

编辑 我尝试使用promises,现在我只是得到错误.我的"简单的"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);
  });
Run Code Online (Sandbox Code Playgroud)

https://jsfiddle.net/abalter/bjwc1dx1/

epa*_*llo 5

jQuery的ajax()文档:

完成

请求完成时要调用的函数(执行成功和错误回调之后)

现在使用现代版本的jQuery,监听成功和错误的首选方法是使用promise接口.

$.ajax(...)
   .done( function() {})     //success
   .fail( function() {})     //error
   .always( function() {});  //complete
Run Code Online (Sandbox Code Playgroud)