Cou*_*uim 2 javascript soap xmlhttprequest cors wildfly
POST我正在尝试使用 javascript 中的方法发送 xmlXmlHttpRequest object.
On my server I've a web service which receives SOAP request.
OPTIONS当我想发送 xml 时,浏览器之前尝试向服务器发送预检请求,但它返回OPTIONS 405 Method Not Allowed.
问题是我的响应标头中有,Access-Control-Method-Allowed : POST,OPTIONS,GET,PUT所以我猜我的服务器接受 OPTIONS 方法,但我的 Web 服务只理解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);
Run Code Online (Sandbox Code Playgroud)
这是我的 HTTP 协议请求标头:
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
Run Code Online (Sandbox Code Playgroud)
这是我的 HTTP 协议响应标头:
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
Run Code Online (Sandbox Code Playgroud)
有什么建议 ?
问题是我的响应标头中有 Access-Control-Method-Allowed :POST、OPTIONS、GET、PUT 所以我猜我的服务器接受 OPTIONS 方法
不。
这只是意味着,当您响应放入该标头的任何请求时,您就是在告诉浏览器发出跨域 OPTIONS 请求是可以接受的。
这绝对不会让您的服务器响应 OPTIONS 请求,200 OK而不是405 Method Not Allowed。
这个答案表明:
@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();
}
Run Code Online (Sandbox Code Playgroud)