REST从jQuery删除:方法不允许错误

时间:2022-12-03 19:33:57

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

我试图使用jQuery Ajax删除记录并调用RESTful服务。但是当我执行时,我收到错误

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

What could be reason for this?

这可能是什么原因?

REST service code

REST服务代码

@Path("/employee")

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

jQuery ajax code

jQuery ajax代码

$(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",
        })
    });
});

4 个解决方案

#1


3  

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

使用DELETE类型并使用url传递empNo。由于删除方法只需要empNo,因此不需要数据,dataType。

$(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",
        })
    });
});

#2


1  

You have not specified empno in the url

您尚未在网址中指定empno

change the url to :http:// localhost:8181/Test1/rest/employee/9870

将URL更改为:http:// localhost:8181 / Test1 / rest / employee / 9870

#3


1  

Why is type not DELETE?

为什么类型不是DELETE?

Also, why is employee number not a part of the path, since you specified it as part of the path with the @Path attribute?

另外,为什么员工编号不是路径的一部分,因为您将其指定为具有@Path属性的路径的一部分?

#4


1  

DELETE is used to delete a resource identified by a URI.

DELETE用于删除由URI标识的资源。

On successful deletion, return HTTP status 200 (OK) along with a response body, perhaps the representation of the deleted item (often demands too much bandwidth), or a wrapped response. Either that or return HTTP status 204 (NO CONTENT) with no response body. In other words, a 204 status with no body, or the JSEND-style response and HTTP status 200 are the recommended responses.

成功删除后,返回HTTP状态200(OK)以及响应正文,可能是已删除项目的表示(通常需要太多带宽)或包装响应。要么是返回HTTP状态204(NO CONTENT)而没有响应正文。换句话说,建议的响应是204状态,没有正文,或JSEND样式响应和HTTP状态200。

You also need to specify empNo as part of your URL

您还需要指定empNo作为URL的一部分

Modify your code as follows and try

修改您的代码如下,然后尝试

 type: 'DELETE',
 url: "http://localhost:8181/Test1/rest/employee" + '/' + empNo,

#1


3  

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

使用DELETE类型并使用url传递empNo。由于删除方法只需要empNo,因此不需要数据,dataType。

$(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",
        })
    });
});

#2


1  

You have not specified empno in the url

您尚未在网址中指定empno

change the url to :http:// localhost:8181/Test1/rest/employee/9870

将URL更改为:http:// localhost:8181 / Test1 / rest / employee / 9870

#3


1  

Why is type not DELETE?

为什么类型不是DELETE?

Also, why is employee number not a part of the path, since you specified it as part of the path with the @Path attribute?

另外,为什么员工编号不是路径的一部分,因为您将其指定为具有@Path属性的路径的一部分?

#4


1  

DELETE is used to delete a resource identified by a URI.

DELETE用于删除由URI标识的资源。

On successful deletion, return HTTP status 200 (OK) along with a response body, perhaps the representation of the deleted item (often demands too much bandwidth), or a wrapped response. Either that or return HTTP status 204 (NO CONTENT) with no response body. In other words, a 204 status with no body, or the JSEND-style response and HTTP status 200 are the recommended responses.

成功删除后,返回HTTP状态200(OK)以及响应正文,可能是已删除项目的表示(通常需要太多带宽)或包装响应。要么是返回HTTP状态204(NO CONTENT)而没有响应正文。换句话说,建议的响应是204状态,没有正文,或JSEND样式响应和HTTP状态200。

You also need to specify empNo as part of your URL

您还需要指定empNo作为URL的一部分

Modify your code as follows and try

修改您的代码如下,然后尝试

 type: 'DELETE',
 url: "http://localhost:8181/Test1/rest/employee" + '/' + empNo,