Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST delete from jQuery : Method Not Allowed error

I am trying to delete a record using jQuery Ajax and caling RESTful service. However when I execute, I am getting error

The specified HTTP method is not allowed for the requested resource 
(Method Not Allowed).

What could be reason for this?

REST service code

@Path("/employee")

@DELETE
@Path("/{empNo}")
@Produces(MediaType.APPLICATION_JSON)
public void remove(@PathParam("empNo") short empNo) {
getEmployeeService().delete(empNo);
}

jQuery ajax code

$(document).ready(function () {
    var empNo = 9870;
    $("#btnSubmit").click(function () {
        $.ajax({
            url: "http://localhost:8181/Test1/rest/employee",
            type: "POST",
            data: JSON.stringify(empNo),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
        })
    });
});
like image 612
Jacob Avatar asked Feb 15 '23 14:02

Jacob


1 Answers

Use DELETE type and pass empNo with url. As delete method only needs empNo, so data, dataType is not needed.

$(document).ready(function () {
    var empNo = 9870;
    $("#btnSubmit").click(function () {
        $.ajax({
            url: "http://localhost:8181/Test1/rest/employee/" + empNo, // Pass empNo
            type: "DELETE", // Use DELETE
           // data: JSON.stringify(empNo), Commented these two.
           // dataType: "json",
        })
    });
});
like image 123
Masudul Avatar answered Feb 17 '23 09:02

Masudul