我試圖發送xml與POST
JavaScript中使用XmlHttpRequest
對象。 在我的服務器上,我有一個接收SOAP請求的Web服務。Wild allow允許選項方法,但返回405方法不允許
當我想發送xml時,瀏覽器會嘗試向服務器發送預檢OPTIONS
請求,但它會返回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);
這裏是我的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
這裏是我的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
有什麼建議?
事實上,你是對的。由於我無法訪問Web服務代碼,因此我將使用不使用xmlhttprequest的中間件服務開發其他解決方案:)感謝您的回答 –