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

1 line
2.6 KiB
JSON

{"name":"ajaxSend","type":"method","title":".ajaxSend()","deprecated":null,"removed":null,"desc":"Attach a function to be executed before an Ajax request is sent. 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, ajaxOptions)","type":"Function"}},"examples":{"desc":"Show a message before an Ajax request is sent.","code":"\n$( document ).ajaxSend(function( event, request, settings ) {\n $( \"#msg\" ).append( \"<li>Starting request at \" + settings.url + \"</li>\" );\n});\n"},"longdesc":"\n <p>Whenever an Ajax request is about to be sent, jQuery triggers the <code>ajaxSend</code> event. Any and all handlers that have been registered with the <code>.ajaxSend()</code> method are executed at this time.</p>\n <p>To observe this method in action, set up a basic Ajax load request:</p>\n <pre><code>\n&lt;div class=\"trigger\"&gt;Trigger&lt;/div&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 ).ajaxSend(function() {\n $( \".log\" ).text( \"Triggered ajaxSend handler.\" );\n});\n </code></pre>\n <p>Now, make an Ajax request using any jQuery method:</p>\n <pre><code>\n$( \".trigger\" ).click(function() {\n $( \".result\" ).load( \"ajax/test.html\" );\n});\n </code></pre>\n <p>When the user clicks the element with class <code>trigger</code> and the Ajax request is about to begin, the log message is displayed.</p>\n <p><strong>As of jQuery 1.8, the <code>.ajaxSend()</code> method should only be attached to <code>document</code>.</strong></p>\n <p>All <code>ajaxSend</code> handlers are invoked, regardless of what Ajax request is to be sent. If you must differentiate between the requests, use the parameters passed to the handler. Each time an <code>ajaxSend</code> handler is executed, it is passed the event object, the <code>jqXHR</code> object (in version 1.4, <code>XMLHttpRequest</code>object), and the <a href=\"/jQuery.ajax/\">settings object</a> that was used in the creation of the Ajax request. For example, you can restrict the callback to only handling events dealing with a particular URL:</p>\n <pre><code>\n$( document ).ajaxSend(function( event, jqxhr, settings ) {\n if ( settings.url == \"ajax/test.html\" ) {\n $( \".log\" ).text( \"Triggered ajaxSend handler.\" );\n }\n});\n </code></pre>\n "}]}