简体   繁体   English

“ Ajax调用中出现” NetworkError:405 Method Not Allowed错误“

[英]"NetworkError: 405 Method Not Allowed error in Ajax Call

I have created a REST service and i tested it on Chrome REST Console . 我创建了REST服务,并在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.. 那里工作正常,我得到响应,但是在通过jQuery Ajax消耗响应的同时,我得到"NetworkError: 405 Method Not Allowed error in Ajax CallCross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:64132/Auth. (Reason: CORS request failed). "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).错误Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:64132/Auth. (Reason: CORS request failed).这是我的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.

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. 如果不了解有关如何实现REST服务的详细信息,就不可能针对这两种选择提供具体说明。 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服务部署在某种容器中,则它可能会提供用于实现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标头

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM