2017-02-16 117 views
2

我試圖發送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 

有什麼建議?

回答

3

的問題是,我在我的迴應已經頭的訪問控制法允許的:POST,OPTIONS,GET,PUT,所以我想我的服務器接受OPTIONS方法

這只是意味着,當您響應您要將該標頭放入的任何請求時,您就是告訴瀏覽器可以接受發出跨源OPTIONS請求。

這絕對沒有什麼可以讓你的服務器響應一個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(); 
} 
+0

事實上,你是對的。由於我無法訪問Web服務代碼,因此我將使用不使用xmlhttprequest的中間件服務開發其他解決方案:)感謝您的回答 –