gpt4 book ai didi

java - 为什么我在执行 POST 请求时收到 405 Method Not Allowed

转载 作者:搜寻专家 更新时间:2023-11-01 03:04:21 26 4
gpt4 key购买 nike

我是其他服务的新手。我正在尝试创建一个接受来自客户端的 json 字符串的服务。当我使用 JQuery 调用此服务时出现 405 错误。下面是 ws 的 Java 代码:

这是我向 JERSEY POST RESTFUL Webservice 发送请求的方式。

var orderinfo = {'ordersplitjson': ordersplitjson, 'customer_id': cust_id , 'homedelivery': homedelivery, 'seatnum' :seatnum , 'locationname':location_nam , 'rownum':rownum}; 
var json_data = JSON.stringify(orderinfo);
var ajaxcallquery = $.ajax({
type:'POST',
dataType: 'jsonp',
data: json_data,
contentType: "application/json; charset=utf-8",
url:url+'/OMS/oms1/orderinsertservice',
jsonpCallback:'jsonCallback',
jsonp:false,
success: function(response)
{
},
error: function(jqxhr, status, errorMsg) {
alert('Failed! ' + errorMsg);
}

});


public class OrdersInsertService
{
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces("application/json")
public String getData(OrderInfo order,@Context HttpServletResponse serverResponse)
throws JSONException
{
serverResponse.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
serverResponse.addHeader("Access-Control-Allow-Credentials", "true");
serverResponse.addHeader("Access-Control-Allow-Origin", "*");
serverResponse.addHeader("Access-Control-Allow-Headers", "Content-Type,X-Requested-With");
serverResponse.addHeader("Access-Control-Max-Age", "60");

}
}


package com.util;

public class OrderInfo {

String ordersplitjson;
public String getOrdersplitjson() {
return ordersplitjson;
}
public void setOrdersplitjson(String ordersplitjson) {
this.ordersplitjson = ordersplitjson;
}
public String getCustomer_id() {
return customer_id;
}
public void setCustomer_id(String customer_id) {
this.customer_id = customer_id;
}
public String getHomedelivery() {
return homedelivery;
}
public void setHomedelivery(String homedelivery) {
this.homedelivery = homedelivery;
}
public String getSeatnum() {
return seatnum;
}
public void setSeatnum(String seatnum) {
this.seatnum = seatnum;
}
public String getLocationname() {
return locationname;
}
public void setLocationname(String locationname) {
this.locationname = locationname;
}
public String getRownum() {
return rownum;
}
public void setRownum(String rownum) {
this.rownum = rownum;
}
String customer_id;

String homedelivery;
String seatnum;

String locationname;
String rownum;


}

谁能告诉我如何解决这个问题

enter image description here

我正在使用 Jersey 1,当我使用你的类(class)时,它在 eclipse 中给我一个编译错误,如图中的 hwon

enter image description here

最佳答案

(1) dataType: 'jsonp'应该是 json .摆脱所有jsonp相关的东西。应该是json。 jsonp 将自动更改为 GET 请求,因此会出现 405(方法不允许)。您的资源方法接受 POST。

(2) CORS 问题。您需要一个过滤器来处理飞行前。如果你使用的是 Jersey 1,你可以使用这个类

import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerResponse;
import com.sun.jersey.spi.container.ContainerResponseFilter;

public class CORSFilter implements ContainerResponseFilter {
@Override
public ContainerResponse filter(ContainerRequest request,
ContainerResponse response) {

response.getHttpHeaders().add("Access-Control-Allow-Origin", "*");
response.getHttpHeaders().add("Access-Control-Allow-Headers",
"origin, content-type, accept, authorization");
response.getHttpHeaders().add("Access-Control-Allow-Credentials", "true");
response.getHttpHeaders().add("Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE, OPTIONS, HEAD");

return response;
}
}

然后像这样注册

resourceConfig.getContainerResponseFilters().add(new CORSFilter());

对于 web.xml,添加这个

<init-param>
<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
<param-value>com.yourpackage.CORSFilter</param-value>
</init-param>

<servlet>里面包含 Jersey servlet 的元素

关于java - 为什么我在执行 POST 请求时收到 405 Method Not Allowed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27890700/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com