Angular Post数组导致数据库中的所有NULL

时间:2017-12-05 10:31:23

标签: javascript angularjs azure post

我正在尝试将数据发布到我的表(T-SQL / Azure),但它写的只是NULL而只是一行,甚至不是完整的数组。我已经尝试了$ param,serialize,$ httpParamSerializer等的许多变体。它们都没有写出表中的内容。甚至控制台.log告诉我一切正常。

var data = $scope.test;
console.log(data);
var jsonData = angular.toJson(data);
console.log(jsonData)
var objectToSerialize = { 'object': jsonData };
console.log(objectToSerialize);

$http({
     url: 'http://url/api/UserOrderProductList',
     method: "POST",
     data: $.param(objectToSerialize),
     //data: $.param(data),
     //data: $.param(jsonData),
     //data: $httpParamSerializerJQLike(objectToSerialize),

     headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
     //headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }

      }).success(function (data) {
            alert("done");
      });

这就是我写的:

    var data = {
        "quantity": 0,
        "title": "string",
        "imageFront": "string"
        }

使用相同的匹配表。

调试器中的

是创建201和完整数组的表单数据

来自标题的表单数据:

object:[{"count":1,"title":"Lara croft",imageFront":"20170807234015"},
{"count":1,"title":"Dragon Age","imageFront":"20170807231312"},
{"count":2,"title":"Madden","imageFront":"20170807235148"}]

这就是预览/响应中所说的内容:

{"id":16,"count":null,"imageFront":null,"title":null}

这是后端部分,如何收到请求:

  // POST: api/UserOrders
  [ResponseType(typeof(UserOrderProductList))]
  public async Task<IHttpActionResult> PostUserOrderProductList(UserOrderProductList UserOrderProductList)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.UserOrderProductLists.Add(UserOrderProductList);
        await db.SaveChangesAsync();

        return CreatedAtRoute("DefaultApi", new { id = UserOrderProductList.id }, UserOrderProductList);
    }

发生了什么以及为什么全部写入NULL? 当我写着招摇时,一切都变好了。

2 个答案:

答案 0 :(得分:1)

尝试将[DllImport("user32.dll", SetLastError = true)] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); IntPtr twHandle =(IntPtr) User32.FindWindow("test", null); 设置为Content-Type并将JSON数据传递到您的Web API:

application/json

Web API的参数应该是List而不是Object:

var data = $scope.test;
var jsonData = angular.toJson(data);

$http({
    url: 'http://url/api/UserOrderProductList',
    method: "POST",
    data: jsonData,
    headers: { 'Content-Type': 'application/json' }

}).success(function (data) {
    alert("done");
});

此处回答了类似的问题:Web Api 2 receive json array

答案 1 :(得分:0)

将Arular Ng-Repeat的内容发布到带有WEB API 2后端的数据库表中,感谢Arron Chen。

var data = $scope.test; // CONTENTS OF THE NG-REPEAT
var jsonData = angular.toJson(data);

$http({
    url: 'http://url/api/UserOrderProductList',
    method: "POST",
    data: jsonData,
    headers: { 'Content-Type': 'application/json' }

}).success(function (data) {
   alert("done");
});

使用匹配的后端:

// POST: api/UserOrders
[Route("api/UserOrderProductList/AddOrderList")]
public HttpResponseMessage PostUserOrderProductList(List<UserOrderProductList> UserOrderProductList)
{

   db.UserOrderProductLists.AddRange(UserOrderProductList);
   db.SaveChangesAsync();

   return new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
}