简体   繁体   中英

Angularjs $http post doesnt binding with asp.net web api server model

I have next problem: Angularjs $http post request dont binding body attributes with model on server side. Model created but with null properties. I have tested with Chrome Postman and firefox poster. All works and binding. So problem supposed to be in angullar

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

and server side controller method

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

EDIT User model

    public class User
{

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

I found answer! First of all, there can be used jObject like

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

but then I dissappeared other problem - jObject model was with backaslashes and angular POST request turn into OPTIONS request. The reason was in CORS Deeping in, I found solution in Thinktecture.dll which allow CORS. Futher information for configuration you can found in next article CORS support in WebAPI, MVC and IIS .
And after that, all working! Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM