1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
<!DOCTYPE html>
<html lang='en'><head><meta http-equiv='content-type' content='text/html; charset=UTF-8' /></head><body>
<div class="entry-content">
<div class="entry-title roundTop">
<h1 class="jq-clearfix">.ajaxError()</h1>
<div class="entry-meta jq-clearfix">
Categories:
<span class="category"><a href="http://api.jquery.com/category/ajax/" title="View all posts in Ajax">Ajax</a> > <a href="http://api.jquery.com/category/ajax/global-ajax-event-handlers/" title="View all posts in Global Ajax Event Handlers">Global Ajax Event Handlers</a></span>
</div>
</div>
<div id="ajaxError1" class="entry method">
<h2 class="jq-clearfix roundTop section-title">
<span class="name">.ajaxError( handler(event, jqXHR, ajaxSettings, thrownError) )</span> <span class="returns">Returns: <a class="return" href="http://api.jquery.com/Types/#jQuery">jQuery</a></span>
</h2>
<div class="jq-box roundBottom entry-details">
<p class="desc"><strong>Description: </strong>Register a handler to be called when Ajax requests complete with an error. This is an Ajax Event.</p>
<ul class="signatures"><li class="signature" id="ajaxError-handlerevent- jqXHR- ajaxSettings- thrownError">
<h4 class="name">
<span class="versionAdded">version added: <a href="/category/version/1.0/">1.0</a></span>.ajaxError( handler(event, jqXHR, ajaxSettings, thrownError) )</h4>
<p class="arguement"><strong>handler(event, jqXHR, ajaxSettings, thrownError)</strong>The function to be invoked.</p>
</li></ul>
<div class="longdesc">
<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.</p>
<p>To observe this method in action, set up a basic Ajax load request.</p>
<pre><button class="trigger">Trigger</button>
<div class="result"></div>
<div class="log"></div></pre>
<p>Attach the event handler to any element:</p>
<pre>$("div.log").ajaxError(function() {
$(this).text( "Triggered ajaxError handler." );
});</pre>
<p>Now, make an Ajax request using any jQuery method:</p>
<pre>$("button.trigger").click(function() {
$("div.result").load( "ajax/missing.html" );
});</pre>
<p>When the user clicks the button and the Ajax request fails, because the requested file is missing, the log message is displayed.</p>
<p><strong>Note:</strong> Because <code>.ajaxError()</code> is implemented as a method of jQuery object instances, you can use the <code>this</code> keyword within the callback function to refer to the selected elements.</p>
<p>All <code>ajaxError</code> handlers are invoked, regardless of what Ajax request was completed. To differentiate between the requests, you can 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. If the request failed because JavaScript raised an exception, the exception object is passed to the handler as a fourth parameter. For example, to restrict the error callback to only handling events dealing with a particular URL:</p>
<pre>$( "div.log" ).ajaxError(function(e, jqxhr, settings, exception) {
if ( settings.url == "ajax/missing.html" ) {
$(this).text( "Triggered ajaxError handler." );
}
});</pre>
</div>
<h3>Example:</h3>
<div class="entry-examples" id="entry-examples"><div id="example-0">
<h4><span class="desc">Show a message when an Ajax request fails.</span></h4>
<pre class="prettyprint"><code class="example">$("#msg").ajaxError(function(event, request, settings){
$(this).append("<li>Error requesting page " + settings.url + "</li>");
});</code></pre>
</div></div>
</div>
</div>
</div>
</body></html>
|