locked
ASP.NET WEB API - Ajax - PUT - Undefined when calling the Method RRS feed

  • Question

  • User135423268 posted

    Good Day

    I'm having a problem when I'm calling my PUT Method on Web API, here's my code below.

    WEB API:

    <HttpPut>
    <Route("api/{controller}/putemployee")>
    Public Sub UpdateOurEmployee(<FromBody()> ByVal EmpList as Employees)
    
     using sqlConn as new sqlConnection(connstrEmp)
       
       using sqlCmd as new SqlCommand("sp_UpdateEmployee",sqlConn)
          
          sqlCmd.CommandType = CommandType.StoredProcedure
          
          sqlCmd.Parameters.AddWithValue("@EmpID",EmpList.EmpID)
          sqlCmd.Parameters.AddWithValue("@FirstName",EmpList.FirstName)
          sqlCmd.Parameters.AddWithValue("@MiddleName",EmpList.MiddleName)
          sqlCmd.Parameters.AddWithValue("@LastName",EmpList.LastName)
          
          sqlConn.Open()
          sqlCmd.ExecuteNonQuery()
    
       end using
     end using
    
    
    End Sub

    Here's my code on Jquery/Ajax

    var EmpList {EmpID: objEmpID, FirstName: objFirstName, MiddleName: objMiddleName, LastName: objLastName}
    
    $.ajax({
      url: '/api/myapi/putemployee',
    data: JSON.stringify(EmpList),
    type: 'PUT',
    contentType: 'application/json',
    success: function (response) {
    alert(response + ' success');
    },
    error: function (xhr) {
    alert(xhr);
    } });

    Wierd is  I have no problem on my PUT in API, there is no error or on my method, the error shows during the alert of success, "response" shows "undefined",

    I have solve this by removing the alert() on under success, but this is bothered me, that It may cause a problem or there some error that i don't see.

    I hope you can give me advise on this.

    Thanks and regards

    Wednesday, August 8, 2018 2:32 AM

All replies

  • User1881638666 posted

    Hi,

    Here the api call just update and return nothing (no response body).

    https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT

    Hence as the caller,(http client) received the 200 OK status with no body.

    You can safely remove 'response' parameter from the success callback.

    $.ajax({
      url: '/api/myapi/putemployee',
      data: JSON.stringify(EmpList),
      type: 'PUT',
      contentType: 'application/json',
      success: function () {
        // invoke logic after success response recieved.
      },
      error: function (xhr) {
        alert(xhr);
      }

     Thanks & Regards,

    Wenushka

    Wednesday, August 8, 2018 5:04 PM