gpt4 book ai didi

javascript - Wildfly 允许 OPTIONS 方法但返回 405 不允许方法

转载 作者:行者123 更新时间:2023-11-29 19:06:45 24 4
gpt4 key购买 nike

我正在尝试使用 XmlHttpRequest 对象在 javascript 中通过 POST 方法发送 xml。在我的服务器上,我有一个接收 SOAP 请求的 Web 服务。

当我想发送 xml 时,浏览器之前尝试向服务器发送预检 OPTIONS 请求,但它返回 OPTIONS 405 Method Not Allowed

问题是我的响应 header 中有 Access-Control-Method-Allowed : POST,OPTIONS,GET,PUT 所以我猜我的服务器接受 OPTIONS 方法,但我的网络服务只理解 POST 请求。

这是一些代码:

 var xmlhttp = new XMLHttpRequest();
xmlhttp.open('POST', url, false);
var sr = mySoapRequest; //Here's my XML

xmlhttp.onreadystatechange = () => {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
var xml = xmlhttp.responseXML;
console.log(xml);
this.showAlert(xml);
}
}
}
xmlhttp.setRequestHeader("content-type", "file/xml");
xmlhttp.send(sr);

这是我的 HTTP 协议(protocol)请求 header :

Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:fr-FR,fr;q=0.8,en;q=0.6,en-US;q=0.4
Access-Control-Request-Headers:content-type
Access-Control-Request-Method:POST
Connection:keep-alive
DNT:1
Host:192.168.149.127
Origin:http://192.168.149.1:8100
Referer:http://192.168.149.1:8100/?ionicplatform=android

这是我的 HTTP 协议(protocol)响应 header :

Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:accept, authorization, content-type, x-requested-with
Access-Control-Allow-Methods:GET, POST, OPTIONS, PUT
Access-Control-Allow-Origin:*
Access-Control-Max-Age:1
Connection:keep-alive
Content-Length:224
Content-Type:text/xml;charset=UTF-8
Date:Thu, 16 Feb 2017 10:25:33 GMT
Server:WildFly/8
X-Content-Type-Options:nosniff
X-FRAME-OPTIONS:SAMEORIGIN
X-Powered-By:Undertow/1
X-XSS-Protection:1

有什么建议吗?

最佳答案

The problem is that I've in my response header the Access-Control-Method-Allowed : POST,OPTIONS,GET,PUT so I guess my server accepts OPTIONS method

没有。

这只是意味着,当您响应放入该 header 的任何请求时,您就是在告诉浏览器发出跨域 OPTIONS 请求是可以接受的。

这绝对不会让您的服务器以 200 OK 响应 OPTIONS 请求,而不是 405 Method Not Allowed

This answer建议:

@OPTIONS
@Path("{path : .*}")
public Response options() {
return Response.ok("")
.header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Headers", "origin, content-type, accept, authorization")
.header("Access-Control-Allow-Credentials", "true")
.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD")
.header("Access-Control-Max-Age", "1209600")
.build();
}

关于javascript - Wildfly 允许 OPTIONS 方法但返回 405 不允许方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42271486/

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