注意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' 情況下發(fā)數(shù)組時,需要設(shè)置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 );
//結(jié)果: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 );
//結(jié)果a[one]=1&a[two]=2&a[three]=3&b[]=1&b[]=2&b[]=3
Notice
這時的b數(shù)組在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