Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to modify XMLHttpRequest data from beforeSend callback?

Is it possible to modify the data sent in an Ajax request by modifying the XMLHttpRequest object in the beforeSend callback? and if so how might I do that?

like image 757
veilig Avatar asked Dec 24 '10 15:12

veilig


People also ask

What is beforeSend function ()?

The beforeSend function is a pre-request callback function that runs before the request is sent to the server. The beforeSend() function use to set the custom headers and all, it is an Ajax event that triggers before an Ajax request is started.

What is the difference between XMLHttpRequest and Ajax?

XMLHttpRequest is the raw browser object that jQuery wraps into a more usable and simplified form and cross browser consistent functionality. jQuery. ajax is a general Ajax requester in jQuery that can do any type and content requests.

What is the purpose of XMLHttpRequest in Ajax?

XMLHTTPRequest is basically used in Ajax programming. It retrieve any type of data such as json, xml, text etc. It request for data in background and update the page without reloading page on client side. An object of XMLHTTPRequest is used for asynchronous communication between client and server.

What does an instance of XMLHttpRequest allows?

XMLHttpRequest is a built-in browser object that allows to make HTTP requests in JavaScript. Despite having the word “XML” in its name, it can operate on any data, not only in XML format. We can upload/download files, track progress and much more.


2 Answers

Yes you can modify it, the signature of beforeSend is actually (in jQuery 1.4+):

beforeSend(XMLHttpRequest, settings)

even though the documentation has just beforeSend(XMLHttpRequest), you can see how it's called here, where s is the settings object:

if ( s.beforeSend && s.beforeSend.call(s.context, xhr, s) === false ) {

So, you can modify the data argument before then (note that it's already a string by this point, even if you passed in an object). An example of modifying it would look like this:

$.ajax({
  //options...
  beforeSend: function(xhr, s) {
    s.data += "&newProp=newValue";
  }
});

If it helps, the same signature applies to the .ajaxSend() global handler (which does have correct documentation showing it), like this:

$(document).ajaxSend(function(xhr, s) {
  s.data += "&newProp=newValue";
});
like image 185
Nick Craver Avatar answered Oct 05 '22 23:10

Nick Craver


I was looking for this solution and wonder why I am not finding the s.data so I changed the request type to post and it was there, Looks like if you are using GET request the data property is not there, I guess you have to change the s.url

for get method:

$.ajax({
  type:'GET',
  beforeSend: function(xhr, s) {
    s.url += "&newProp=newValue";
  }
});

for post method:

$.ajax({
  type:'POST',
  beforeSend: function(xhr, s) {
    s.data += "&newProp=newValue";
  }
});
like image 39
talsibony Avatar answered Oct 06 '22 00:10

talsibony