using ajax beforeSend to modify data

This blog post explains how you can use $.ajaxSetup to add data. It accumulates like $.extend Just do this:

$.ajaxSetup({
  data:{
    isAjax:true
  }
});

$.ajaxSetup({
  beforeSend: function(jqXHR, settings) {
    settings.data = $.extend(settings.data, {isAjax: true});
    return true;
  }
});

==== UPDATE ====

When data is an object, jQuery generates the data string from the object's key/value pairs unless the processData option is set to false. For example, { a: "bc", d: "e,f" } is converted to the string "a=bc&d=e%2Cf". If the value is an array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below). For example, { a: [1,2] } becomes the string "a%5B%5D=1&a%5B%5D=2" with the default traditional: false setting.

https://api.jquery.com/jquery.ajax/

As you can see, the setting processData should to be false. So it can be setted when you do request in $.ajax() or globally in $.ajaxSetup().


You can just actually use beforeSend in $.ajax();

$.ajax({
  beforeSend: function(xhr){
    this.data += '&' + $.param({
      param: 'test'
    });
  }
});

Tags:

Ajax

Jquery