jQuery .ajax() POST 请求在 RESTful WCF 上抛出 405(不允许方法)
我正在向 RESTFUL WCF 服务应用程序发送发布请求.我能够通过 Fiddler 成功发送 POST
请求.
I am sending a post request to a RESTFUL WCF service application. I am able to successfully send a POST
request through Fiddler.
但是,当我通过 jQuery Ajax 方法执行此操作时,该函数会向 Chrome 开发者控制台返回以下内容:
However when I do this through the jQuery Ajax method the function returns the following to the Chrome Developer Console:
OPTIONS http://www.example.com/testservice/service1.svc/GetData 405 (Method Not Allowed) jquery.min.js:6
但是在记录之后的一秒钟:
But then a second after logs:
Object {d: "You entered 10"} testpost.html:16
这告诉我的是 jQuery 正在发送一个 OPTIONS
请求,该请求失败,然后发送一个 POST
请求返回预期数据.
What this tells me is that jQuery is sending a OPTIONS
request, which fails, and then sending a POST
request which returns the expected data.
我的 jQuery 代码:
My jQuery Code:
$.ajax() {
type: "POST", //GET or POST or PUT or DELETE verb
url: "http://www.example.com/testservice/service1.svc/GetData", // Location of the service
data: '{"value":"10"}', //Data sent to server
contentType:"application/json",
dataType: "json", //Expected data format from server
processdata: false,
success: function (msg) {//On Successfull service call
console.log(msg);
},
error: function (xhr) { console.log(xhr.responseText); } // When Service call fails
});
我使用的是 jQuery 2.0.2 版.
I am using jQuery version 2.0.2.
有关为什么会发生此错误的任何帮助都会有很大帮助.
Any help on why this error is occurring would be a great help.
您的代码实际上是在尝试创建一个跨域(CORS)请求,不是普通的POST
.
也就是说:现代浏览器只允许 Ajax 调用与 HTML 页面在相同域中的服务.
That is: Modern browsers will only allow Ajax calls to services in the same domain as the HTML page.
示例: http://www.example.com/myPage.html
中的页面只能直接请求 http://中的服务www.example.com
,例如 http://www.example.com/testservice/etc
.如果服务在其他域中,浏览器将不会进行直接调用(如您所料).相反,它会尝试发出 CORS 请求.
Example: A page in http://www.example.com/myPage.html
can only directly request services that are in http://www.example.com
, like http://www.example.com/testservice/etc
. If the service is in other domain, the browser won't make the direct call (as you'd expect). Instead, it will try to make a CORS request.
简而言之,要执行 CORS 请求,您的浏览器:
To put it shortly, to perform a CORS request, your browser:
- 将首先向目标 URL 发送
OPTION
请求 - 然后仅当服务器对该
OPTION
的响应包含足够的标头(Access-Control-Allow-Origin
是其中之一) 以允许 CORS 请求,浏览器将执行调用(几乎与 HTML 页面位于同一个域).- 如果没有出现预期的标头,浏览器就会放弃(就像对您所做的那样).
- Will first send an
OPTION
request to the target URL - And then only if the server response to that
OPTION
contains the adequate headers (Access-Control-Allow-Origin
is one of them) to allow the CORS request, the browse will perform the call (almost exactly the way it would if the HTML page was at the same domain).- If the expected headers don't come, the browser simply gives up (like it did to you).
如何解决?最简单的方法是在服务器上启用 CORS(启用必要的标头).
How to solve it? The simplest way is to enable CORS (enable the necessary headers) on the server.
如果您没有服务器端访问权限,您可以从其他地方镜像 Web 服务,然后在那里启用 CORS.
If you don't have server-side access to it, you can mirror the web service from somewhere else, and then enable CORS there.