javascript 如何在 ajax 直接传递数组呢?
比如说,我有这样的数组:
var arr = [1,2,3];
在 $.ajax({})里面是不能直接传过去的!
不过呢,如果把数组弄成是对象来操作的话,就没问题了,具体例子如下:
var arr = {};
arr['block_301'] = {};
arr['block_302'] = {};
arr['block_301']['block_301_1'] = 'block_301_1';
arr['block_301']['block_301_2'] = 'block_301_2';
arr['block_302']['block_302_1'] = 'block_302_1';
arr['block_302']['block_302_2'] = 'block_302_2';
$.ajax({
url: 'http://your_url.com',
data: arr,
type: 'POST'
})
arr['block_301'] = {};
arr['block_302'] = {};
arr['block_301']['block_301_1'] = 'block_301_1';
arr['block_301']['block_301_2'] = 'block_301_2';
arr['block_302']['block_302_1'] = 'block_302_1';
arr['block_302']['block_302_2'] = 'block_302_2';
$.ajax({
url: 'http://your_url.com',
data: arr,
type: 'POST'
})
传递的时候就变成:
block_301[block_301_1]:block_301_1
block_301[block_301_2]:block_301_2
block_302[block_302_1]:block_302_1
block_302[block_302_2]:block_302_2
block_301[block_301_2]:block_301_2
block_302[block_302_1]:block_302_1
block_302[block_302_2]:block_302_2