tags:

views:

969

answers:

1

I found strange bug (maybe). When I send parameters to $.ajax in a form of hash, and tried to check that params in $.ajaxSend, I found that settings.data is null, settings.url was normal with parameters included. Then I look inside jQuery code, and found that data erased.

 // If data is available, append data to url for get requests
 if ( s.data && type == "GET" ) {
  s.url += (s.url.match(/\?/) ? "&" : "?") + s.data;

  // IE likes to send both get and post data, prevent this
  s.data = null;
 }

Now I am in need of parsing url ((. What to do?

http://stackoverflow.com/questions/73885/in-jquery-using-ajaxsend-to-preview-the-url-built-by-post-call

Here in comments I see that data should be there.

+1  A: 

By default anything passed as data that is not a String is processed and transformed into a query string. So, if you use POST to skip the bug:

 $.post({
      url: "http://yourserver/ajax" ,
      data: {param1: 'val1', param2:'val2'}
 });

in $.ajaxSend the value of settings.data will be 'param1=val1&param2=val2' and you will need to parse the parameters, like with GET.

If you want to avoid parsing the url or data just add a copy of the data when building the settings object. The extra parameter should not cause any problem.

 var data = {param1: 'val1', param2:'val2'};
 $.get({
      url: "http://yourserver/ajax" ,
      data: data,
      dataCopy: data
 });

Then, in $.ajaxSend you can check the values in settings.dataCopy.

Serhii