jQuery.ajaxPrefilter() JQ Home  <<  Ajax  <<  jQuery.ajaxPrefilter()

Type / Setting pre-filtering.

Description

The jQuery.ajaxPrefilter() Ajax method, allows us to filter custom Ajax settings or modify the existing settings, before sending each request and before they are processed by the $.ajax() method.

Shorthand version $.ajaxPrefilter()

  • All subsequent Ajax requests using any function will use the new settings, unless overridden by the individual requests, until the next invocation of the $.ajax() method.

Syntax

Signature Description
jQuery.ajaxPrefilter( [dataTypes], handler( settings, localSettings, jqXHR) )Filter custom Ajax settings or modify the existing settings, before sending each request and before they are processed by the $.ajax() method.

Parameters

Parameter Description Type
dataTypes An optional string containing one or more space-separated dataTypes.String
settings Ajax settings.

Function
localSettings Settings as provided to the ajax method, unmodified and thus without defaults from ajax Settings.
jqXHR The jqXHR object of the request.

Return

undefined.

jQuery.ajaxPrefilter( [dataTypes], handler(settings, localSettings, jqXHR) ) ExamplesAjax  <<  Top

Filter custom Ajax settings or modify the existing settings, before sending each request and before they are processed by the $.ajax() method.

In the example below when we press the left button the first time, we create a custom setting using the jQuery.ajaxSetup() method. We then run the userRequest() function and within that create a deferred/promise to map to the output from a request. After this we call the jQuery.ajaxPrefilter() method and abort any request to the page not from a special user. We output a message confirming this.

In the example below when we press the right button the first time, we create a custom setting using the jQuery.ajaxSetup() method. We then run the userRequest() function and within that create a deferred/promise to map to the output from a request. After this we call the jQuery.ajaxPrefilter() method and accept the request to the page from a special user. We output a message confirming this, return the Promise of the jqXHR and then output messages accordingly.


$(function(){
  $('#btn7').one('click', function(){
    $.ajaxSetup({
      specialUser: false
    });
    checkUser();
  });
  $('#btn8').one('click', function(){
    $.ajaxSetup({
     specialUser: true
    });
    checkUser();
  });
  function checkUser() {
    returnedPromise = userRequest();
    /*
     * Here we can check our returned promise which we mapped
     * from our Ajax request and if done is a special user.
     */
    returnedPromise.done( function() {
      $('#div3').append('Special user! <br>>+++++++++++<br><br>');
    });
    returnedPromise.fail( function() {
      $('#div3').append('No special user! <br>>+++++++++++<br><br>');
    });
  }
  
  function userRequest() {
    var ourDeferred = new $.Deferred();
    var userPromise = ourDeferred.promise(); 

    $.ajaxPrefilter( function(settings, localSettings, jqXHR) {
      if(settings.specialUser) {
	    $('#div3').append('Special user setting exists! <br>');
      } else {
  	    $('#div3').append('No special user setting, abort! <br>');
  	    jqXHR.abort();  
      }
    });
    var jqxhr = $.ajax({
       type: "GET",
       url: "../../../js/get3.js",
       dataType: "script"
    });
    jqxhr.done(function(data, status, xhr) {
      $('#div3').append('Special user with prefilter, so resolve the deferred! <br>');
      ourDeferred.resolve();
    });
    jqxhr.fail(function(jqXHR, status, error) {
      $('#div3').append('Error with Ajax request! ' + status + ' ' + error + ' <br>');
      ourDeferred.reject();
    });
    // Return the jqXHR Promise object
    return userPromise;
  }
});

/*
 * The code for the external Javascript file called from 
 * $.ajax() (url: "../../../js/get3.js") is shown below.
 */
$(function(){
  var someText = 'Special users only. ';
  $('#div3').append('Message from get3.js: ' + someText + '
'); });

div3. Some initial text.

Press the button below to action the above code:

               

Related Tutorials

jQuery Advanced Tutorials - Lesson 8 - Ajax Low-Level Interface