Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a general parameter to all ajax calls made with jQuery

I'm using AJAX to load data from the server as needed. I'm currently working on updating the server software to the latest version. One of the things I noticed that has changed is that every request now requires me to pass along a token. This means I have to add a new parameter to every request. I'm hoping to achieve this by a general method, without having to modify every single AJAX call.

Any pointers?

like image 866
Peeter Avatar asked Sep 01 '11 13:09

Peeter


People also ask

Which parameters are being used for the jQuery AJAX method?

The parameters specifies one or more name/value pairs for the AJAX request. The data type expected of the server response. A function to run if the request fails. A Boolean value specifying whether a request is only successful if the response has changed since the last request.

Can AJAX be used with jQuery?

jQuery provides several methods for AJAX functionality. With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post - And you can load the external data directly into the selected HTML elements of your web page!

Which three parameters are used in the sequence of AJAX call?

context: It is used to specify the “this” value for all AJAX-related callback functions. data: It is used to specify data to be sent to the server. dataFilter(data, type): It is used to handle the raw response data of the XMLHttpRequest.


2 Answers

You could use the $.ajaxSetup method as illustrated in the following article.

like image 112
Darin Dimitrov Avatar answered Oct 14 '22 04:10

Darin Dimitrov


What you're looking for is a Prefilter, here's a sample from the page:

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

This requires JQuery 1.5.

like image 39
Maurício Linhares Avatar answered Oct 14 '22 05:10

Maurício Linhares