我正在尝试在我的 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); } }); };