0

我了解到 $.ajax() 返回一个 jqXHR 对象,您可以在该对象上调用许多函数。我们甚至可以调用 jqXHR.abort() 来中止 AJAX 调用。

我的问题是如何捕获 ajax 调用被中止并在调用 .abort() 时通知用户。

一个简单的例子表示赞赏

4

1 回答 1

0

You can try ajaxError method should only be attached to document.

like :

$( document ).ajaxError(function( event, jqxhr, settings, exception ) {
  if ( settings.url == "ajax/missing.html" ) {
    $( "div.log" ).text( "Triggered ajaxError handler." );
  }
});

Or you can try error method: A function to be called if the request fails.

jQuery.ajax({
      type: "POST",
      url: "url",
      dataType: "type",
      data: "data",
      success: function (response) {
         alert("Success");
      },
      error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
      }
    });

Check APIDocumentation for detailed information about error method

于 2013-10-17T16:29:24.223 回答