2017-07-06 99 views
1

當用戶單擊保存按鈕時,JavaScript函數使用AJAX調用Controller並通過有關對象的JSON數據發送。ajax控制器中的空參數

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

JSON $ 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" 
     } 
    ] 
}; 

固定控制器對於N.Ivanov的溶液(該控制器+ N的伊萬諾夫的阿賈克斯=解決方案)

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; 
    } 
+0

使它像這樣,讓我知道'數據:{「項目」:JSON.stringify($ scope.items.data) },' –

回答

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

我已驗證{ "item": $scope.items.data }是一個有效的JSON通過JSONLint

希望這能解決您的問題!

+0

你也可以像這樣嘗試:'data:{item:JSON.stringify($ scope.items。數據)},'希望這有助於! 「 –

+1

」data:$ scope.items.data「fires」JSON primitive:Item1「無效。和數據:{item:JSON.stringify($ scope.items.data)}會觸發「無效的JSON基元:item」。像數據引用:{「item」:JSON.stringify($ scope.items.data)}引發同樣的錯誤。變量與函數具有相同的範圍。以JSON格式添加my $ scope.items.data的數據以提問 – Ryu

+0

@Ryu我已更新我的答案,檢查*編輯*,希望它能解決問題! –

0

嘗試JSON解析解析數據對象,並將其發送到控制器

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