Skip to content

How to use PUT method using AJAX to call WEB API controller

Tanmay Mule

Asked Feb 11, 2015, 1:20 AM EST5 answers
Share
LinkedInX
I want to update record in database by using WEB API Controller

 // PUT api/Student/5
        [HttpPut]
        public HttpResponseMessage PutStudent(int id, [FromBody]Student student)
        {
        //body
        }

then how should I call this method using jquery ajax put method?

4 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    I got Internal Server Error 500 because of JSON.stringify(student)...
    even the execution doesn't reach to my controllers method
    0
  • Pranay Rana
    0
    please check the url you are passing to ajax function is correct, i updated in my answer also but you can check at your end and also refer this article : http://www.codeproject.com/Articles/424461/Implementing-Consuming-ASP-NET-WEB-API-from-JQuery
    0
  • 0
    when im trying to debug my program then at controller's method im getting student parameter value NULL
    0
  • Pranay Rana
    -1
    you can try like this


    var student = new Object();
    student =
    {
      FirtName : 'John',
      LastName : 'Smith'
    };

    $.ajax(
      {
          url: 'api/Student/PutStudent/' + id,
          type: 'Put',
      contentType : "application/json"
      data: JSON.stringify(datasend),
        dataType: "json",     
      success: function(result)
      {
              // Do something with the result
          }
     });


    • type (default: 'GET')
      Type: String
      The type of request to make (e.g. "POST", "GET", "PUT"); default is "GET".

    Check jquery document : http://api.jquery.com/jQuery.ajax/#options for more details
    -1
  • 1 short reply
    • Pranay Rana
      0
      is that worked for you
      0