How does $ .ajaxError work?

I use $ .get, $ .post and $ .getJSON extensively, so I wanted to see how .ajaxError works. I tried this ...

$(document).ajaxError(function (e, xhr, options, error)
{
    console.log(e);
    console.log(xhr);
    console.log(options);
    console.log(error);
});

and I also tried to put it in mine $(document).ready().. And then I disconnected the network connection. AJAX made a lot of errors POST(because they received unexpected answers), and GETreturned instantly (cached), but nothing appeared in Firebug, as if ajaxError had never been called, What am I doing wrong? I just tried again without console logs and simply alert('foo'), and again nothing happened. Many errors, but ajaxError never fires.

+3
source share
3 answers

ajaxError , XHR HTTP , jQuery. ; .

jQuery , ajaxError, XMLHTTPRequest readyState 4 ( ), , XHR (, 2?). , jQuery .

, , , , , ajaxError, , jQuery, .

+6

jQuery.

http://api.jquery.com/ajaxError/

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>jQuery Sandbox</title>
  </head>
  <body>
    <div class="trigger">Trigger</div>
    <div class="result"></div>
    <script type="text/javascript" src="../scripts/jquery-1.4.js"></script>
    <script type="text/javascript">

        $(function () {

            $(document).ajaxError(function (e, xhr, settings, exception) {
                console.log(xhr.status);
            });

            $('.trigger').click(function () {
                $('.result').load('ajax/thisUrlDoesNotExist.html');
            });

        });

    </script>
  </body>
</html>

?

+1

If the ajax call made is a cross-domain, ajaxError is not triggered when the call fails.

0
source

Source: https://habr.com/ru/post/1752293/


All Articles