Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "traditional style of param serialization' in JQuery

Tags:

jquery

Do you know what is "traditional style of param serialization" for jQuery.ajax() as mentioned in http://api.jquery.com/jQuery.ajax/ ?

Can you give some introduction?

Thanks

like image 927
Ricky Avatar asked Mar 31 '11 08:03

Ricky


People also ask

What is the use of param () method in jQuery?

The param() method creates a serialized representation of an array or an object. The serialized values can be used in the URL query string when making an AJAX request.

What is traditional in Ajax call?

The traditional property changes the way how parameters are sent to the server. As of jQuery 1.8, it is defaulted to false. For ASP.NET MVC developer $. ajax(url, { data : { a : [1,2,3] }, traditional : true })); // `data` are sent as "a=1&a=2&a=3"


1 Answers

Have a look at the documentation of jQuery.param():

As of jQuery 1.4, the $.param() method serializes deep objects recursively to accommodate modern scripting languages and frameworks such as PHP and Ruby on Rails. You can disable this functionality globally by setting jQuery.ajaxSettings.traditional = true;.

Given

var p = {foo: [1,2,3], bar: 42};

setting traditional to true generates

foo=1&foo=2&foo=3&bar=42

While e.g. Python can handle these parameters, i.e. it generates a list for foo, PHP will only consider the last foo parameter.

But now by default, the result of the serialization is (actually it is URI encoded)

foo[]=1&foo[]=2&foo[]=3&bar=42

which can be better handled, as mentioned, by PHP and RoR.


Or maybe even more interesting is this. Given:

var p = {foo: {1: [3,4], 2:2,3:3}, bar: 42};

traditional produces:

foo=[object Object]&bar=42

which is clearly not useful in comparison with the "new" way:

foo[1][]=3&foo[1][]=4&foo[2]=2&foo[3]=3&bar=42
like image 104
Felix Kling Avatar answered Oct 16 '22 14:10

Felix Kling