我的数组是这样的:
Array[2]
0: Object
StockNo: "1"
InvoiceNo: "1234"
MaterialName: "MaterialName1"
PONo: "1234"
PRNo: "2124"
Project: "ProjectName"
Qty: "1"
Remarks: "Test"
Supplier: "SupplierName"
TotalAmount: "23"
Type: "2"
Unit: "23"
1: Object
StockNo: "2"
InvoiceNo: "1234"
MaterialName: "MaterialName2"
PONo: "1234"
PRNo: "2124"
Project: "ProjectName2"
Qty: "1"
Remarks: "Test"
Supplier: "SupplierName"
TotalAmount: "23"
Type: "2"
Unit: "23"
并且这个2数组位于名为“inventoryList”的变量中 我想让它通过我的控制器。我用了
JSON.stringify(inventoryList)
但它不起作用。
这是我的剧本:
function addSomething() {
var dateReceived = $('#DateReceived').val();
$.ajax({
url: "/MyController/Create",
type: 'post',
dataType: 'json',
async: false,
data: {
'dateReceived': dateReceived,
'rrList': JSON.stringify(inventoryList)
},
success: function (data) {
if (data.errorMessage != '') {
alert(data.errorMessage);
}
else {
window.location.href = '/MyController/Index';
}
}
})
}
答案 0 :(得分:0)
将JSON对象更改为
inventoryList=JSON.stringify({ 'inventoryList': inventoryList});
然后在您的控制器中
public void Create(DateTime dateReceived,List<object> inventoryList)
{
}
答案 1 :(得分:0)
我认为应该有效。
在我看来,发送数据的内容类型应为application/json
,async不应设置为false,因为它会破坏ajax的行为。
所以我的建议是:
function addSomething() {
var dateReceived = $('#DateReceived').val();
$.ajax({
url: "/MyController/Create",
type: 'post',
dataType: 'json',
contentType: 'application/json', // <----add the content type
//async: true, // you can remove it because default is true.
data: {
'dateReceived': dateReceived,
'rrList': JSON.stringify(inventoryList)
},
success: function (data) {
if (data.errorMessage != '') {
alert(data.errorMessage);
}
else {
window.location.href = '/MyController/Index';
}
}
})
}
答案 2 :(得分:0)
<html>
<head>
<title>Ajax Json</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<h1>Well Come</h1>
<hr>
<div id="data">Response Add Hear...!!!</div>
</body>
<script type="text/javascript">
$(document).ready(function(){
$obj1={"StockNo":"1","InvoiceNo":"1123","MaterialName":"MaterialName1"};
$obj2={"StockNo":"1","InvoiceNo":"1124","MaterialName":"MaterialName2"};
$obj_array=[];
$obj_array.push($obj1);
$obj_array.push($obj2);
$obj_array=JSON.stringify($obj_array);
$.ajax({
url:'test.php',
method:'post',
async:false,
data:{"test":$obj_array},
success:function(data){
$("#data").html(data);
}
});
});
</script>
</html>
服务器文件
<?php
echo "<pre>";
print_r(json_decode($_REQUEST['test']));
echo "</pre>";
?>