NetworkError: 405 Ajax调用中Method Not Allowed错误
"NetworkError: 405 Method Not Allowed error in Ajax Call
我已经创建了一个REST
服务,并在Chrome REST Console
上进行了测试。那里的工作很好,我得到的响应,但同时消耗相同的jquery Ajax我得到"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).
错误。这是我的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");
}
});
请帮我纠正这个错误并成功呼叫服务。谢谢…
更新当我试图从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.
现代浏览器对于从不同的"域"检索内容有非常严格的规则。从本地文件系统(HTML/脚本)检索内容与向localhost
(ajax调用)发出网络请求不同,因此Chrome将其视为跨域请求。
为了使跨域请求成功,您需要从http://localhost:64132/
URL也提供HTML内容/脚本(因此它们在同一域中),或者您需要设置REST服务来实现CORS舞蹈,以允许您的file:
URL访问它。
如果不知道REST服务如何实现的细节,就不可能给出任何一个选项的具体说明。选择哪一种将取决于您打算如何在现实生活中部署页面和服务(是否相同的域)以及实现的难易程度。如果您的REST服务部署在某种容器中,它可能提供实现CORS和/或服务初始页面的选项。
相同问题的随机示例(尽管这里的确切解决方案不太可能适用于您的特定情况):当Origin 'null'当尝试向LOCAL应用程序发送数据时
- jQuery is not loaded
- AngularJS JSON not arriving php
- $rootScope not working
- reactRedux is not defined
- jQuery document.ready not working
- Javascript/Jquery Blob not showing Chrome PDF
- ReferenceError: not defined
- ReferenceError: cordova is not defined @ng-cordova.min.js:7
- Javascript getElementsByTagName not working?
- jQuery's trim()的前缀为not运算符
- Javascript - element.childNodes does not see an append.newch
- SemanticUI模态not onDeny/onApprove事件未激发
- WebkitTransform not fluent
- JQuery.val( ) not working
- document.applet.method在Mounatin Lion上抛出safari 6+JDK7异常
- 当method=post时,jQueryAjax返回404
- Laravel with vuejs v-on not calling method
- NetworkError: 405 Ajax调用中Method Not Allowed错误
- Method not Allowed Error in Jquery Ajax Method used
- KnockoutJS fromJS not working TypeError: Cannot call method