当用户单击保存按钮时,JavaScript函数使用AJAX调用Controller并发送有关对象的JSON数据 .
JavaScript Function
$.ajax({
url: "/Data/sendFridgeItems?items=" + JSON.stringify($scope.items.data),
type: "POST",
data: JSON.stringify($scope.items.data),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function () {
console.log("good!");
},
error: function () {
console.log("error");
}
});
Controller
[HttpPost]
public ActionResult SendFridgeItems(string items)
{
fridge[] fridgeItems = JsonConvert.DeserializeObject<fridge[]>(items);
foreach (fridge item in fridgeItems)
{
bool exists = cookDB.fridges.AsEnumerable()
.Any(x => x.Name == item.Name && x.Purchased == item.Purchased && x.Count == item.Count);
if (!exists)
{
cookDB.fridges.Add(item);
cookDB.SaveChangesAsync();
}
}
return null;
}
它有效,但我不认为通过url参数发送数据的方式在我的情况下是正确的,因为数据将足够大 . 我想知道是否有更好的方法将数据发送到控制器?
我尝试以这种方式发送它,但控制器接收空值 .
$.ajax({
url: "/Data/sendFridgeItems",
type: "POST",
data: JSON.stringify($scope.items.data),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function () {
console.log("good!");
},
error: function () {
console.log("error");
}
});
JSON of $scope.items.data
[{"id":2,"name":"Item1","count":2,"type":"pcs.","purchased":"12/09/2017","wasted":"15/10/2017","cam":"Freezer","comment":"no comment","$$hashKey":"object:38"},{"id":3,"name":"Item2","count":30,"type":"g.","purchased":"15/01/1880","wasted":"21/03/1882","cam":"Cooler","comment":"commented","$$hashKey":"object:39"}]
$scope.items
$scope.items = {
"count": 2,
"data": [
{
"name": "Item1",
"count": 2,
"type": "pcs.",
"purchased": "12/09/2017",
"wasted": "15/10/2017",
"cam": "Freezer",
"comment": "no comment"
},
{
"name": "Item2",
"count": 30,
"type": "g.",
"purchased": "15/01/1880",
"wasted": "21/03/1882",
"cam": "Cooler",
"comment": "Commented"
}
]
};
Fixed Controller For N.Ivanov's solution (this controller+ N.Ivanov's ajax = solution)
public ActionResult SendFridgeItems(fridge[] items)
{
fridge[] fridgeItems = JsonConvert.DeserializeObject<fridge[]>(items.ToString());
foreach (fridge item in items)
{
bool exists = cookDB.fridges.AsEnumerable()
.Any(x => x.Name == item.Name && x.Purchased == item.Purchased && x.Count == item.Count);
if (!exists)
{
cookDB.fridges.Add(item);
cookDB.SaveChangesAsync();
}
}
return null;
}
2 回答
ajax中的
data
字段接受一个Object,你给它一个字符串 . 假设$scope.items.data
是一个对象,请尝试并仅提供您的对象 . 如果你提供一些关于$scope
变量的更多信息,那么我可以给你一个更好的答案 .Code:
$.ajax({ url: "/Data/sendFridgeItems", type: "POST", d̶a̶t̶a̶:̶ ̶$̶s̶c̶o̶p̶e̶.̶i̶t̶e̶m̶s̶.̶d̶a̶t̶a̶,̶ dataType: "json", contentType: "application/json; charset=utf-8", success: function () { console.log("good!"); }, error: function () { console.log("error"); } });
希望这可以帮助!
EDIT:
在提供
$scope.items.data
的内容后进一步检查,我发现$scope.items.data
是一个对象数组 . 因此,为了使ajax工作并实际提供有效的JSON,请尝试以下代码:$.ajax({ url: "/Data/sendFridgeItems", type: "POST", data: { "items": $scope.items.data }, dataType: "json", contentType: "application/json; charset=utf-8", success: function () { console.log("good!"); }, error: function () { console.log("error"); } });
我已通过JSONLint验证
{ "item": $scope.items.data }
是有效的JSON希望这能解决你的问题!
尝试使用JSON Parse将数据解析为Object并将其发送到控制器