Igor Golodnitsky
Igor Golodnitsky

Reputation: 4566

jQuery $.ajaxSend() settings.data is null

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?

In JQuery, using ajaxSend to preview the url built by $.post call

Here in comments I see that data should be there.

Upvotes: 1

Views: 2666

Answers (1)

Serxipc
Serxipc

Reputation: 6699

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.

Upvotes: 1

Related Questions