注意ajax中的traditional;
jQuery ajax
data
Type: PlainObject or String or Array
Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).
如果是 'GET' 情况下发数组时,需要设置traditional,那么我们看看traditional setting
traditional
Type: Boolean
Set this to true
if you wish to use the traditional style of param serialization.
最后看$.param()中给出的例子明显可以看出,
var myObject = {
a: {
one: 1,
two: 2,
three: 3
},
b: [ 1, 2, 3 ]
};
var recursiveEncoded = $.param( myObject );
var recursiveDecoded = decodeURIComponent( $.param( myObject ) );
alert( recursiveEncoded );
//结果:a%5Bone%5D=1&a%5Btwo%5D=2&a%5Bthree%5D=3&b%5B%5D=1&b%5B%5D=2&b%5B%5D=3
alert( recursiveDecoded );
//结果a[one]=1&a[two]=2&a[three]=3&b[]=1&b[]=2&b[]=3
Notice
这时的b数组在url中传递时是b[]
var myObject = {
a: {
one: 1,
two: 2,
three: 3
},
b: [ 1, 2, 3 ]
};
var shallowEncoded = $.param( myObject, true );
var shallowDecoded = decodeURIComponent( shallowEncoded );
alert( shallowEncoded );
//a=%5Bobject+Object%5D&b=1&b=2&b=3
alert( shallowDecoded );
//a=[object+Object]&b=1&b=2&b=3