我正在尝试从ajax GET / POST访问托管在控制台应用程序上的REST服务。由于两个应用程序中将使用不同的端口,因此我已配置REST服务来处理跨源请求。我已经尝试了所有可用的解决方案,但仍然无法从ajax访问该服务并获得405。
我正在分享由http分析器捕获的Raw请求 - 响应流。
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://www.khalidabuhakmeh.com/rest-http-verbs-and-iis-express和{{3}}
答案 0 :(得分: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 强>β-控制-...
并将返回405
方法不允许。
此外,对于GET
,不需要Content-Length
标头。最好删除该行。
答案 1 :(得分:0)
我通过以下指南解决了问题:
尽量避免预检请求more Information(在我的情况下,我从请求中删除了自定义标头。)
如果有,请不要在客户端请求中将withCredentials属性设置为true 在服务上设置Access-Control-Allow-Origin:*。
但是,这只解决了GET请求问题。在POST中,我遇到了同样的问题,但是this solution就像魔术一样。