来自ajax的控制器中的空参数

时间:2017-07-06 10:20:30

标签: javascript jquery ajax asp.net-mvc

当用户单击保存按钮时,JavaScript函数使用AJAX调用Controller并发送有关对象的JSON数据。

JavaScript功能

$.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");
        }
    });

控制器

[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");
        }
    });

$ scope.items.data的JSON

[{"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"
      }
    ]
};

N.Ivanov解决方案的固定控制器(此控制器+ N.Ivanov&#39; ajax =解决方案)

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 个答案:

答案 0 :(得分:1)

ajax中的data字段接受一个Object,你给它一个字符串。假设$scope.items.data是一个对象,请尝试并仅提供您的对象。如果你提供一些关于$scope变量的更多信息,那么我可以给你一个更好的答案。

<强>代码:

$.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"); } });

希望这有帮助!

修改

在您提供$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

希望这能解决你的问题!

答案 1 :(得分:1)

尝试使用JSON Parse将数据解析为Object并将其发送到控制器

$.ajax({
    url: "/Data/sendFridgeItems",
    type: "POST",
    data: JSON.parse($scope.items.data),
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function () {
        console.log("good!");
    },
    error: function () {
        console.log("error");
    }
});