×

ajax传递给后台数组参数方式

96
soser
2016.12.15 11:32* 字数 281

业务需要,有时候需要我们前端传递给后台数组方式,普通的方式显而无法满足要求,那么问题来了,我们如何传递数组给服务端?废话不说,直接上代码。
<pre>
$(function() {
var params = {
name: 'admin',
address: {
province: '浙江',
city: '杭州'
},
fids: [1, 2],
friends: [{
name: '张三',
age: 10
}, {
name: '李四',
age: 15
}]
};
$.ajax({
url: '后台接口地址',
type: 'GET',
data: $.param(serializeObjects(params)),
traditional: true,
success: function(result) {
// 成功函数
},
error: function() {
console.log('error');
}
});
// 对参数进行特殊转化
function serializeObjects(params) {
var obj = {};
for (var k in params) {
var o = params[k];
if ('[object Array]' === Object.prototype.toString.call(o))
for (var i = 0; i < o.length; i++) {
var o1 = o[i];
if ('[object Object]' === Object.prototype.toString.call(o1))
for (var k1 in o1) obj[(k + '[' + i + '].' + k1).toString()] = o1[k1];
else obj[(k + '[' + i + ']').toString()] = o1;
} else if ('[object Object]' === Object.prototype.toString.call(o))
for (var k2 in o) obj[(k + '.' + k2).toString()] = o[k2];
else obj[k.toString()] = o;
}
return obj;
}
})
</pre>
github相关链接:https://github.com/sosout/webPit/blob/master/demos/201612151116.html
个人博客:http://www.sosout.com/

前端
Web note ad 1