简体   繁体   English

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

[英]Why are AJAX complete and error callbacks both firing?

Here is a dirt-simple AJAX request with anonymous callbacks.这是一个带有匿名回调的非常简单的 AJAX 请求。 Both complete and error are firing. completeerror都在触发。 Why?为什么?

$.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/ https://jsfiddle.net/abalter/tz0tu04y/

EDIT I tried using promises, and now I'm only getting error.编辑我尝试使用承诺,现在我只收到错误。 Something must be wrong with my "dirt-simple" ajax.我的“简单的”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);
  });

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

From the jQuery's ajax() documentation :来自jQuery 的 ajax() 文档

complete完全的

A function to be called when the request finishes (after success and error callbacks are executed)请求完成时调用的函数(执行成功和错误回调后)

Now with modern versions of jQuery the preferred method to listen for success and error would be to use promise interface.现在使用现代版本的 jQuery,侦听成功和错误的首选方法是使用 promise 接口。

$.ajax(...)
   .done( function() {})     //success
   .fail( function() {})     //error
   .always( function() {});  //complete

Complete is not the same as success.完成不等于成功。 It fires when the response comes back regardless of the status.无论状态如何,当响应返回时它都会触发。

Update to answer your second question:更新以回答您的第二个问题:

The reason you are getting the error in the first part of the second fiddle is that it is expecting javascript and you gave text that is not valid javascript.您在第二个小提琴的第一部分收到错误的原因是它期望使用 javascript 而您提供的文本不是有效的 javascript。

If you send it to an endpoint that your format is valid for, or put your hello world in quotes (make it valid javascript) it will work.如果您将其发送到您的格式对其有效的端点,或者将您的 hello world 放在引号中(使其有效 javascript),它将起作用。

When you configure an AJAX request you can set several options, among them you define the callbacks for the beforeSend, success, error and complete events.当您配置 AJAX 请求时,您可以设置多个选项,其中您定义 beforeSend、success、error 和 complete 事件的回调。 If you check this http://api.jquery.com/jquery.ajax/ you will see that the complete callback is invoked for both, success and error events.如果您检查此http://api.jquery.com/jquery.ajax/,您将看到为成功和错误事件调用了完整的回调。 Cheers干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM