如何将对象从jquery发送到aspnet webapi控制器?

问题描述 投票:-1回答:1

我尝试在post方法中将对象作为json发送到控制器但是它引发了错误。该怎么办?(附上这个相关代码):

    public class user
    {
        public string username;
        public string password;
    }
    // POST: api/User
    public void Post(user u)
    {
        if (!database_layer.create_user(u.username, u.password))
        { throw new HttpResponseException(HttpStatusCode.BadRequest); }
    }
jquery json asp.net-web-api
1个回答
0
投票

如果要将数据作为JSON发送,则需要在发送之前对其进行序列化。用data:user替换

data: JSON.stringify(user)

请参阅以下已提问之一:

  1. Send JSON data via POST (ajax) and receive json response from Controller (MVC)
  2. Send JSON data with jQuery
  3. jQuery ajax, how to send JSON instead of QueryString
© www.soinside.com 2019 - 2024. All rights reserved.