我正在尝试在我的ajax保存功能中发送多个数据数组。
我可以像data:hardwarePayload
一样单独执行每个数组,它会起作用。如果我做{hardware: hardwarePayload, service:servicePayload}
我得到非常奇怪的JSON输出。看起来像是:
硬件=%5B%7B%22hardwareName%22%3A%221%22%2C%22hardwareQuantity%22%3A%22%22%2C%22hardwareBYOD%22%3A%22%22%7D%5D&安培;服务= %5B%7B%22serviceName%22%3A%223%22%2C%22serviceQuantity%22%3A%22%22%7D%5D
我真的需要两个阵列,一个硬件和一个服务,所以我可以单独抓取每个。
我的代码看起来像这样..
self.save = function (form) { var hardwareModel = []; var serviceModel = []; ko.utils.arrayForEach(self.services(), function (service) { serviceModel.push(ko.toJS(service)); }); ko.utils.arrayForEach(self.hardwares(), function (hardware) { hardwareModel.push(ko.toJS(hardware)); }); //allModel.push({accountId: ko.toJS(account)}); var hardwarePayload = JSON.stringify(hardwareModel); var servicePayload = JSON.stringify(serviceModel); //alert(JSON.stringify(serviceModel) +JSON.stringify(allModel)); $.ajax({ url: '/orders/add', type: 'post', data: {hardware: hardwarePayload, service:servicePayload}, // data:hardwarePayload, contentType: 'application/json', success: function (result) { alert(result); } }); };
答案 0 :(得分:1)
你应该试试这个
var hardwarePayload = hardwareModel;
var servicePayload = serviceModel;
var postData = {'hardware': hardwarePayload, 'service':servicePayload};
var postData = JSON.stringify(postData);
alert(postData);
$.ajax({
url: '/orders/add',
type: 'post',
data: postData,
contentType: 'application/json',
success: function (result) {
alert(result);
}
});
答案 1 :(得分:0)
如果你不字符串你的数据,我认为你会更好:
$.ajax({
url: '/orders/add',
type: 'post',
data: {hardware: hardwareModel, service:serviceModel}, // data:hardwarePayload,
contentType: 'application/json',
success: function (result) {
alert(result);
}
});
(请注意,我使用不字符串化 hardwareModel 和 serviceModel )
这样你就可以让jQuery处理请求的(json)数据。