2016-01-05 32 views
1

我想從ajax GET/POST訪問託管在控制檯應用程序上的REST服務。由於在兩個應用程序中將使用不同的端口,因此我配置了REST服務來處理跨源請求。我嘗試了所有可用的解決方案,但仍然無法從ajax訪問服務並獲得405.如何處理Http Error 405方法不允許?

我正在共享由http分析器捕獲的原始請求 - 響應流。

OPTIONS /Service/Contacts?_145***************** HTTP/1.1 
Accept: */* 
Origin: http:localhost:1053 
Access-Control-Request-Method: GET 
Access-Control-Request-Headers: access-control-allow-origin, access-control-allow-methods 
Accept-Encoding: gzip, deflate 
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko 
Host: bhawesh-pc:8000 
DNT: 1 
Connection: Keep-Alive 
Cache-Control: no-cache 
Content-Length: 0 
HTTP/1.1 405 Method Not Allowed 
Allow: POST, GET 
Content-Type: text/html; charset=UTF-8 
Server: Microsoft-HTTPAPI/2.0 
Access-Control-Allow-Origin: * 
Access-Control-Request-Method: POST,GET,PUT,DELETE,OPTIONS 
Access-Control-Allow-Headers: X-Requested-With,Content-Type 
Date: Tue, 05 Jan 2016 17:57:44 GMT 
Content-Length: 1565 

<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <title>Service</title> 
    <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style> 
    </head> 
    <body> 
    <div id="content"> 
     <p class="heading1">Service</p> 
     <p>Method not allowed.</p> 
    </div> 
    </body> 
</html> 

REST服務配置是基於http://enable-cors.org/server_wcf.htmlhttp://www.khalidabuhakmeh.com/rest-http-verbs-and-iis-express

+1

您的服務處理'OPTIONS'請求嗎? –

+0

正如@ 1.618所暗示的那樣,您似乎看到了這條消息,因爲您的瀏覽器正在向[CORS預檢](https://developer.mozilla.org/zh-CN)的一部分發送「OPTIONS」 US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests),但該服務不支持「OPTIONS」請求。 (並且您的瀏覽器發送CORS預檢「OPTIONS」請求的原因是每個[CORS定義的規範](https://fetch.spec.whatwg.org/),原始請求的性質如此它需要一個CORS預檢。) – sideshowbarker

+0

響應頭Access-Control-Allow-Origin:*,訪問控制請求方法:POST,GET,PUT,DELETE,OPTIONS,Access-Control-Allow-Headers:X-Requested- With,Content-Type是在服務端以編程方式設置的,如鏈接中所述。請建議我還需要做什麼。 –

回答

0

我解決了這些準則的問題:

儘量避免預檢要求more Information(在我的情況,我刪除了從要求定製標題。)

不要設置withCredentials屬性爲true在客戶端請求,如果你有 設置訪問控制允許來源:*服務。

但是,這隻解決了GET請求問題。在POST中,我遇到了同樣的問題,但this solution像魔術一樣工作。

0

這不是有效的HTTP GET請求:

Access-Control-Request-Method: GET 
Access-Control-Request-Headers: access-control-allow-origin, access-control-allow-methods 
Accept-Encoding: gzip, deflate 
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko 
Host: bhawesh-pc:8000 
DNT: 1 
Connection: Keep-Alive 
Cache-Control: no-cache 
Content-Length: 0 

一個GET請求的第一行應是:

GET /wanted_resource HTTP/1.1 

因此,如果服務器着眼於在頭的開始請求的方法,它看到這一點:

ACCE SS-控制-...

,並會返回一個405方法不允許。

此外,與GET,沒有Content-Length標頭是必要的。刪除該行最好。

+0

我懷疑OP意味着「訪問控制請求方法:GET」是發送HTTP GET請求正文的第一行。相反,它似乎是問題的一部分只是指出哪些請求標頭被髮送。 – sideshowbarker

+0

謝謝你們倆。我錯過了三條線。我現在糾正了。 –