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",
})
});
});
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",
})
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With