【发布时间】:2013-11-23 19:58:52
【问题描述】:
我正在使用 chrome 尝试访问我的本地服务器。 还使用 jquery-2.0.3 和 jquery-mobile-1.4.0-rc。 我的页面网址是: file:///C:/Users/....../index.html
我的代码如下所示:
var login = {
in: function(){
var ajaxUrl = "http:// localhost/Account/Login/";
var loginModel = {
UserName: $("#userMame").val(),
Password: $("#password").val(),
RememberMe: true
};
$.mobile.loading('show');
$.ajax({
type: "POST",
url: ajaxUrl,
dataType: 'json',
data: JSON.stringify(loginModel),
contentType: "application/json;charset=windows-1255",
scriptCharset: "windows-1255",
headers : {Accept : "application/json",
"Access-Control-Allow-Origin" : "*",
"Access-Control-Allow-Methods": "POST,GET",
"Access-Control-Allow-Headers": "X-Authorization,X-Requested-With"},
crossDomain : true,
success: login.inSuccess,
error: login.inFailure
});
},
inSuccess: function(obj) {
alert("Success");
},
inFailure: function(obj, b, c) {
alert("Failure");
}
}
$(document).bind('pageinit', function () {
$.support.cors = true;
$.mobile.allowCrossDomainPages = true;
});
我在调用 login.in() 时不断收到同样的错误:
OPTIONS http://localhost/Account/Login/ Origin null 是 Access-Control-Allow-Origin 不允许的。
如您所见,我已经尝试了所有方法。 . .
【问题讨论】:
-
您是通过服务器访问您的页面(即
http://localhost)还是在本地打开它(即file://...)?要使 AJAX 调用正常工作,您需要使用第一种方法。 -
如果你在没有firefox的情况下测试你的代码,就不会出现这个问题。
标签: jquery ajax google-chrome jquery-mobile cross-domain