【问题标题】:Angularjs $http post doesnt binding with asp.net web api server modelAngularjs $http post 不与 asp.net web api 服务器模型绑定
【发布时间】:2015-02-17 16:08:46
【问题描述】:

我有下一个问题: Angularjs $http 发布请求不将正文属性与服务器端的模型绑定。已创建模型但具有空属性。 我已经用 Chrome Postman 和 firefox 海报进行了测试。所有作品和绑定。所以问题应该出在角度

 local.signup = function(user) {
      $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
      return $http.post(config.signupUrl, {displayName:user.displayName,email:user.email, password: user.password})
        .then(function(response) {
          if (config.loginOnSignup) {
            shared.setToken(response);
          } else if (config.signupRedirect) {
            $location.path(config.signupRedirect);
          }
          return response;
        });
    };

和服务器端控制器方法

    [HttpOptions]
    [HttpPost]
    [Route("signup")]
    public IHttpActionResult Signup([FromBody] User model)
    {
        if (model == null || String.IsNullOrWhiteSpace(model.email))
            return BadRequest("Email Address is required.");

        if (String.IsNullOrWhiteSpace(model.displayName))
            return BadRequest("Name is required.");
            ......
         }

编辑 用户模型

    public class User
{

    public string displayName { get; set; }
    public string email { get; set; }
    public string password { get; set; }
}

【问题讨论】:

  • 你能发布你的User类结构吗?
  • @OmriAharon 是的,当然。我已经编辑了。

标签: javascript c# asp.net angularjs rest


【解决方案1】:

我找到了答案! 首先,可以使用jObject之类的

 public IHttpActionResult Post([FromBody] jObject model){..}

但后来我消失了其他问题 - jObject 模型带有反斜杠,角度 POST 请求变成了 OPTIONS 请求。原因在CORS 深入研究,我在 Thinktecture.dll 中找到了允许 CORS 的解决方案。您可以在下一篇文章CORS support in WebAPI, MVC and IIS中找到更多配置信息。
之后,一切正常! 祝你好运!

【讨论】:

    猜你喜欢
    • 2014-04-18
    • 2012-08-28
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-23
    • 1970-01-01
    • 2012-10-10
    相关资源
    最近更新 更多