【问题标题】:Origin null is not allowed by Access-Control-Allow-Origin cross-domainAccess-Control-Allow-Origin 跨域不允许 Origin null
【发布时间】: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


【解决方案1】:

出于安全原因,JavaScript 对文件系统的访问 客户受到限制 - 考虑您是否想要(某人 else's) JavaScript 来读取您的敏感文档。

即使在试验时,最好使用真实的拓扑结构, 从服务器提供的东西将在 真实系统。

设置一个像Apache这样的Web服务器来指向它真的很容易 您的开发目录,因此“服务器”只是您的桌面 伪装。因此编辑/测试周期非常快。

Using AJAX to read local files

【讨论】:

    猜你喜欢
    • 2012-06-07
    相关资源
    最近更新 更多