MVC controller method not receiving json data from $http post

前端 未结 3 1273
北荒
北荒 2021-01-24 01:17

This might be basic question but I couldn\'t find where the issue is. I am using .Net Core for by back end and angularJS for front end. below is my code.

<
相关标签:
3条回答
  • 2021-01-24 01:36

    Just Change your

    data: { email: scope.email, password: scope.password },
    

    to

    data: { "email": scope.email, "password": scope.password },
    
    0 讨论(0)
  • 2021-01-24 01:37

    You must create an Class or Interface to send several parameters

    public class PostParams
    {
        public string email { get; set; }
        public string password { get; set; }
    }     
    [HttpPost]
    public JsonResult loginInfo(PostParams params)
    {
      //do something here
    
    }
    
    0 讨论(0)
  • 2021-01-24 01:42

    You have to use the [FromBody] attribute with a model parameter.

    public class Credentials
    {
        public string Email { get; set; }
        public string Password { get; set; }
    }
    
    public IActionResult Login([FromBody]Credentials creds)
    {
        // do stuff
    }
    

    This is because in ASP.NET Core MVC the default content type is application/x-www-form-urlencoded (for handling HTML form data). The [FromBody] attribute tells the model binder to instead try and create the model from JSON content.

    0 讨论(0)
提交回复
热议问题