【问题标题】:Posting AngularJS JSON Array ASP.NET Web API Error发布 AngularJS JSON 数组 ASP.NET Web API 错误
【发布时间】:2016-05-10 12:17:41
【问题描述】:

这似乎是一个常见问题,我已经研究并尝试了我今天在网络上可以找到的所有内容,但我仍然无法让我的帖子正常工作。使用带有 chrome 的开发人员工具,我可以查看请求有效负载,它包含我尝试发布的数据,但随后出现错误

“无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型 'CertAngular.cert_unit_Angular',因为该类型需要 JSON 对象(例如 {"name":"value"})才能正确反序列化。 要修复此错误,请将 JSON 更改为 JSON 对象(例如 {"name":"value"})或将反序列化类型更改为数组或实现集合接口的类型(例如 ICollection、IList),例如可以从 JSON 数组反序列化。 JsonArrayAttribute 也可以添加到类型中以强制它从 JSON 数组中反序列化。 路径'',第 1 行,位置 1。”

开发者工具中头部包含的Request Payload是

[{"cc_id":7778,"unit_id":"TEST1","$$hashKey":"object:5"}]

我已经尝试了几乎所有我能找到的方法来让它工作,但我仍然在挣扎。我假设这是我忽略或不太理解的简单事情

在我向可用客户端添加一些数据后,我在 AngularJS 中的 HTTP 帖子的代码是这样完成的...

$scope.availableclients = [];


$scope.submitCertData = function () {

    var objdata = JSON.stringify($scope.availableclients);

    $http.post('api/PostAttempt2/Postcert_unit_Angular/', objdata)
    .success(function () {

        console.log("Posted");
    })

}

我正在为“带有动作的 Web API 2 控制器,使用实体框架”使用模板后控制器。代码如下......(除了添加 [FromBody] 并尝试之外,我留下了 Visual Studio 的创建方式其他人建议的组合。)

// POST: api/PostAttempt2
[ResponseType(typeof(cert_unit_Angular))]
public IHttpActionResult Postcert_unit_Angular([FromBody]cert_unit_Angular cert_unit_Angular)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.cert_unit_Angular.Add(cert_unit_Angular);

        try
        {
            db.SaveChanges();
        }
        catch (DbUpdateException)
        {
            if (cert_unit_AngularExists(cert_unit_Angular.unit_id))
            {
                return Conflict();
            }
            else
            {
                throw;
            }
        }

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

我的 WebApi 路由配置是...

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

        config.Routes.MapHttpRoute(
            name: "DefaultAPI",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }

感谢任何帮助,我可以提供任何必要的信息。谢谢。

这里是 cert_unit_Angular 类。对不起,我错过了。

     public class cert_unit_Angular
    {
        public string unit_id { get; set; }
        public Int32 cc_id { get; set; }
    }

【问题讨论】:

  • 如果在发送数据之前不进行字符串化怎么办?
  • 贴出cert_unit_Angular(c#)类的定义。
  • [FromBody]cert_unit_Angular[] cert_unit_Angular 没有/不起作用(忽略字符串化)?也许我错过了一些东西,但是您正在将一个数组传递给一个不接受数组的 webapi 控制器操作。
  • 在我发送数据之前删除 stringify 仍然让我收到同样的错误。
  • 我的想法是,如果我对数组执行 JSON.stringify,它将在我的 webapi 控制器中接受 JSON,但似乎并非如此。我将课程添加到我原来的帖子中!

标签: c# asp.net angularjs json asp.net-web-api


【解决方案1】:

你的 web api 需要这样的东西 {"unit_id":"ncn","cc_id":5} 你需要的是它是一个数组

所以定义这样的东西作为方法签名

public IHttpActionResult Postcert_unit_Angular([FromBody]List<cert_unit_Angular> unitID)

public IHttpActionResult Postcert_unit_Angular([FromBody]cert_unit_Angular[] unitID)

【讨论】:

  • 好的,非常感谢。我现在可以使用您提到的第一种方式使用 WebAPI 查看我的数据。我是否必须定义数组的结构以适应表格所期望的结构?
  • 我不确定我是否理解该评论,但您指的是哪个表? html表还是db表?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-30
  • 2013-02-08
  • 2016-08-09
相关资源
最近更新 更多