javascript “网络错误:Ajax 调用中的 405 方法不允许错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32651745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"NetworkError: 405 Method Not Allowed error in Ajax Call
提问by Lara
I have created a REST
service and i tested it on Chrome REST Console
. There its working fine and i am getting the response but while consuming the same by jquery Ajax i am getting "NetworkError: 405 Method Not Allowed error in Ajax Call
and Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:64132/Auth. (Reason: CORS request failed).
error.Here is my Ajax code..
我创建了一个REST
服务并在Chrome REST Console
. 还有它的做工精细,我得到响应,但同时消耗相同的jQuery阿贾克斯我得到"NetworkError: 405 Method Not Allowed error in Ajax Call
和Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:64132/Auth. (Reason: CORS request failed).
error.Here是我的Ajax代码..
var UserDetails = { "UserName": "Bond", "Password": "password" };
$.ajax({
type: 'POST',
url: 'http://localhost:64132/Auth',
crossDomain: true,
data: UserDetails,
contentType: "application/json; charset=utf-8",
success: function (data) {
alert(data.UserName);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("some error");
}
});
Please help me to correct this and successfully call the Service.Thanks..
请帮我改正并成功致电服务中心。谢谢..
Update
更新
when i am trying to call the Service from Chrome i am getting following error in console..
当我尝试从 Chrome 调用服务时,我在控制台中收到以下错误..
Failed to load resource: the server responded with a status of 405 (Method Not Allowed)
Test.html:1 XMLHttpRequest cannot load http://localhost:64132/Auth. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 405.
采纳答案by CupawnTae
Modern browsers have very strict rules about retrieving content from different "domains". Retrieving content from your local file system (your HTML/script) is not the same as making a network request to localhost
(your ajax call), so Chrome treats it as a cross-domain request.
现代浏览器对从不同“域”检索内容有非常严格的规则。从本地文件系统(您的 HTML/脚本)检索内容与向localhost
(您的 ajax 调用)发出网络请求不同,因此 Chrome 将其视为跨域请求。
In order for the cross-domain request to succeed, you either need to serve the HTML content/script from a http://localhost:64132/
URL as well (so they're in the same domain), or you need to set up your REST service to implement the CORS dance to allow your file:
URL to access it.
为了使跨域请求成功,您还需要从http://localhost:64132/
URL提供 HTML 内容/脚本(因此它们在同一个域中),或者您需要设置 REST 服务来实现 CORS跳舞以允许您的file:
URL 访问它。
Without knowing the details of how your REST service is implemented, it's impossible to give specific instructions for either option. Which you choose will will depend on how you intend to deploy the page and service in real life (same domain or not) and ease of implementation. If your REST service is deployed in some sort of container it may provide options for implementing CORS, and/or for serving the initial page.
如果不知道您的 REST 服务是如何实现的细节,就不可能为任一选项提供具体说明。您选择哪个将取决于您打算如何在现实生活中部署页面和服务(相同域与否)以及实施的难易程度。如果您的 REST 服务部署在某种容器中,它可能会提供用于实现 CORS 和/或用于提供初始页面的选项。
Random example of the same issue (although the exact solution here is unlikely to work for your specific case): Access-Control-Allow-Origin header when Origin 'null' when trying to post data to a LOCAL application
同一问题的随机示例(尽管此处的确切解决方案不太可能适用于您的特定情况):尝试将数据发布到 LOCAL 应用程序时 Origin 'null' 时的 Access-Control-Allow-Origin 标头