-1

I tried to send object as json to controller in post method but it's raise error. what's to do?(attatched this relvant code):

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

1 Answers1

0

If your data is to be send as JSON you need to serialize it before it is send. Replace data:user with

data: JSON.stringify(user)

See one of the following already asked questions:

  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
Severin Jaeschke
  • 661
  • 7
  • 22
  • Well I'm pleased I could help. You should mark the answer as correct So others can find it easily. Also on a little tangent, you should edit your original Post so that it contains the information you have now provided as an answer. – Severin Jaeschke Feb 28 '18 at 14:10