control-freak-ide/Control-Freak-Documentation/jQuery/docs/entries/ajaxError.json
plastic-hub-dev-node-saturn 538369cff7 latest
2021-05-12 18:35:18 +02:00

1 line
2.9 KiB
JSON

{"name":"ajaxError","type":"method","title":".ajaxError()","deprecated":null,"removed":null,"desc":"Register a handler to be called when Ajax requests complete with an error. This is an <ahref=\"/Ajax_Events/\">Ajax Event</a>.","categories":["ajax/global-ajax-event-handlers","version/1.0"],"entries":[{"return":"jQuery","signatures":{"added":"1.0","argument":{"desc":"The function to be invoked.","name":"handler(event, jqXHR, ajaxSettings, thrownError)","type":"Function"}},"examples":{"desc":"Show a message when an Ajax request fails.","code":"\n$( document ).ajaxError(function( event, request, settings ) {\n $( \"#msg\" ).append( \"<li>Error requesting page \" + settings.url + \"</li>\" );\n});\n"},"longdesc":"\n <p>Whenever an Ajax request completes with an error, jQuery triggers the <code>ajaxError</code> event. Any and all handlers that have been registered with the <code>.ajaxError()</code> method are executed at this time. <strong>Note:</strong> <em>This handler is not called for cross-domain script and cross-domain JSONP requests.</em></p>\n <p>To observe this method in action, set up a basic Ajax load request.</p>\n <pre><code>\n&lt;button class=\"trigger\"&gt;Trigger&lt;/button&gt;\n&lt;div class=\"result\"&gt;&lt;/div&gt;\n&lt;div class=\"log\"&gt;&lt;/div&gt;\n </code></pre>\n <p>Attach the event handler to the document:</p>\n <pre><code>\n$( document ).ajaxError(function() {\n $( \".log\" ).text( \"Triggered ajaxError handler.\" );\n});\n </code></pre>\n <p>Now, make an Ajax request using any jQuery method:</p>\n <pre><code>\n$( \"button.trigger\" ).on( \"click\", function() {\n $( \"div.result\" ).load( \"ajax/missing.html\" );\n});\n </code></pre>\n <p>When the user clicks the button and the Ajax request fails, because the requested file is missing, the log message is displayed.</p>\n <p><strong>As of jQuery 1.8, the <code>.ajaxError()</code> method should only be attached to <code>document</code>.</strong></p>\n <p>All <code>ajaxError</code> handlers are invoked, regardless of what Ajax request was completed. To differentiate between the requests, use the parameters passed to the handler. Each time an <code>ajaxError</code> handler is executed, it is passed the event object, the <code>jqXHR</code> object (prior to jQuery 1.5, the <code><abbr title=\"XMLHttpRequest\">XHR</abbr></code> object), and the settings object that was used in the creation of the request. When an HTTP error occurs, the fourth argument (<code>thrownError</code>) receives the textual portion of the HTTP status, such as \"Not Found\" or \"Internal Server Error.\" For example, to restrict the error callback to only handling events dealing with a particular URL:</p>\n <pre><code>\n$( document ).ajaxError(function( event, jqxhr, settings, exception ) {\n if ( settings.url == \"ajax/missing.html\" ) {\n $( \"div.log\" ).text( \"Triggered ajaxError handler.\" );\n }\n});</code></pre>\n "}]}