Sathya
Sathya

Reputation: 5308

jquery ajax - add a common param to each ajax request

I want to add a param on each ajax request globally. So tried with ajaxSend. But there is no data object in settings (3rd parameter). Is there a way to add a common param on each request. Request may be in any type (GET, POST, etc...).

I tried like this.

$( document ).ajaxSend(function(event, jqxhr, settings) {
   //i have to manipulate data object here. So i can add an additional param. 
   //OR i need equivalent to this.
});

Upvotes: 2

Views: 581

Answers (2)

Sathya
Sathya

Reputation: 5308

I added extra param like below.

$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
     options.url = options.url + "?appId=" + appId;
});

But changing originalOptions data object not taking any effect. So i'm not sure what i did above work in all case. But for me, it works in all types (GET, POST etc).

Upvotes: 0

jbl
jbl

Reputation: 15413

Have a look at ajaxPrefilter

$.ajaxPrefilter(function( options, originalOptions, jqXHR ) {
// Modify options, control originalOptions, store jqXHR, etc
});

It will allow you to process the request and update any option before the request is issued

Upvotes: 2

Related Questions