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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
<!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">jQuery.ajaxPrefilter()</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/low-level-interface/" title="View all posts in Low-Level Interface">Low-Level Interface</a></span>
</div>
</div>
<div id="jQuery-ajaxPrefilter1" class="entry method">
<h2 class="jq-clearfix roundTop section-title">
<span class="name">jQuery.ajaxPrefilter( [dataTypes] , handler(options, originalOptions, jqXHR) )</span> <span class="returns">Returns: <a class="return" href="http://api.jquery.com/Types/#undefined">undefined</a></span>
</h2>
<div class="jq-box roundBottom entry-details">
<p class="desc"><strong>Description: </strong>Handle custom Ajax options or modify existing options before each request is sent and before they are processed by $.ajax().</p>
<ul class="signatures"><li class="signature" id="jQuery-ajaxPrefilter-dataTypes-handleroptions- originalOptions- jqXHR">
<h4 class="name">
<span class="versionAdded">version added: <a href="/category/version/1.5/">1.5</a></span>jQuery.ajaxPrefilter( [dataTypes], handler(options, originalOptions, jqXHR) )</h4>
<p class="arguement"><strong>dataTypes</strong>An optional string containing one or more space-separated dataTypes</p>
<p class="arguement"><strong>handler(options, originalOptions, jqXHR)</strong>A handler to set default values for future Ajax requests.</p>
</li></ul>
<div class="longdesc">
<p>A typical prefilter registration using <code>$.ajaxPrefilter()</code> looks like this:</p>
<pre>
$.ajaxPrefilter( function( options, originalOptions, jqXHR ) {
// Modify options, control originalOptions, store jqXHR, etc
});
</pre>
<p>where:</p>
<ul>
<li>
<code>options</code> are the request options</li>
<li>
<code>originalOptions</code> are the options as provided to the ajax method, unmodified and, thus, without defaults from <code>ajaxSettings</code>
</li>
<li>
<code>jqXHR</code> is the jqXHR object of the request</li>
</ul>
<p>Prefilters are a perfect fit when custom options need to be handled. Given the following code, for example, a call to <code>$.ajax()</code> would automatically abort a request to the same URL if the custom <code>abortOnRetry</code> option is set to <code>true</code>:</p>
<pre>
var currentRequests = {};
$.ajaxPrefilter(function( options, originalOptions, jqXHR ) {
if ( options.abortOnRetry ) {
if ( currentRequests[ options.url ] ) {
currentRequests[ options.url ].abort();
}
currentRequests[ options.url ] = jqXHR;
}
});
</pre>
<p>Prefilters can also be used to modify existing options. For example, the following proxies cross-domain requests through http://mydomain.net/proxy/:</p>
<pre>
$.ajaxPrefilter( function( options ) {
if ( options.crossDomain ) {
options.url = "http://mydomain.net/proxy/" + encodeURIComponent( options.url );
options.crossDomain = false;
}
});
</pre>
<p>If the optional <code>dataTypes</code> argument is supplied, the prefilter will be only be applied to requests with the indicated dataTypes. For example, the following only applies the given prefilter to JSON and script requests:</p>
<pre>
$.ajaxPrefilter( "json script", function( options, originalOptions, jqXHR ) {
// Modify options, control originalOptions, store jqXHR, etc
});
</pre>
<p>The <code>$.ajaxPrefilter()</code> method can also redirect a request to another dataType by returning that dataType. For example, the following sets a request as "script" if the URL has some specific properties defined in a custom <code>isActuallyScript()</code> function:</p>
<pre>
$.ajaxPrefilter(function( options ) {
if ( isActuallyScript( options.url ) ) {
return "script";
}
});
</pre>
<p>This would ensure not only that the request is considered "script" but also that all the prefilters specifically attached to the script dataType would be applied to it.</p>
</div>
</div>
</div>
</div>
</body></html>
|