首页 文章

XMLHttpRequest Origin null不允许Access-Control-Allow-Origin for file:/// to file:///(Serverless)

提问于
浏览
204

我正在尝试创建一个可以下载并通过启动其索引文件在本地运行的网站 .

所有文件都是本地的,没有资源在线使用 .

当我尝试使用jQuery的AJAXSLT插件来处理带有XSL模板的XML文件时(在子目录中),我收到以下错误:

XMLHttpRequest cannot load file:///C:/path/to/XSL%20Website/data/home.xml. Origin null is not allowed by Access-Control-Allow-Origin.

XMLHttpRequest cannot load file:///C:/path/to/XSL%20Website/assets/xsl/main.xsl. Origin null is not allowed by Access-Control-Allow-Origin.

发出请求的索引文件是 file:///C:/path/to/XSL%20Website/index.html ,而使用的JavaScript文件存储在 file:///C:/path/to/XSL%20Website/assets/js/ 中 .

我该怎么做才能解决这个问题?

8 回答

  • 174

    对于无法运行本地网络服务器的情况,您可以允许Chrome通过浏览器开关访问 file:// 文件 . 经过一番挖掘后,我找到了this discussion,其中提到了开放帖子中的浏览器切换 . 运行您的Chrome实例:

    chrome.exe --allow-file-access-from-files
    

    This may be acceptable for development environments, but little else. 你当然不希望这样 . 这仍然是一个悬而未决的问题(截至2011年1月) .

    另见:Problems with jQuery getJSON using local files in Chrome

  • 3

    基本上解决这个问题的唯一方法是让一个webserver在localhost上运行并从那里为它们提供服务 .

    浏览器允许ajax请求访问计算机上的任何文件是不安全的,因此大多数浏览器似乎将"file://"请求视为没有来源“Same Origin Policy

    启动Web服务器可以像在文件所在和运行的目录中一样简单:

    python -m SimpleHTTPServer
    
  • 4

    这是一个AppleScript,它将启动Chrome并打开--allow-file-access-from-files开关,因为OSX / Chrome开发了:

    set chromePath to POSIX path of "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"    
    set switch to " --allow-file-access-from-files"
    do shell script (quoted form of chromePath) & switch & " > /dev/null 2>&1 &"
    
  • 4

    此解决方案允许您使用jQuery.getScript()加载本地脚本 . 这是一个全局设置,但您也可以基于每个请求设置crossDomain选项 .

    $.ajaxPrefilter( "json script", function( options ) {
      options.crossDomain = true;
    });
    
  • 1

    如何使用javascript FileReader函数打开本地文件,即:

    <input type="file" name="filename" id="filename">
    <script>
    $("#filename").change(function (e) {
      if (e.target.files != undefined) {
        var reader = new FileReader();
        reader.onload = function (e) {
            // Get all the contents in the file
            var data = e.target.result; 
            // other stuffss................            
        };
        reader.readAsText(e.target.files.item(0));
      }
    });
    </script>
    

    现在单击 Choose file 按钮并浏览到文件 file:///C:/path/to/XSL%20Website/data/home.xml

  • 86

    像这样启动chrome以绕过此限制: open -a "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" --args --allow-file-access-from-files .

    源自Josh Lee's comment但我需要指定谷歌浏览器的完整路径,以避免从我的Windows分区(在Parallels中)打开谷歌浏览器 .

  • 1

    您可以尝试将 'Access-Control-Allow-Origin':'*' 放在 response.writeHead(, {[here]}) 中 .

  • 4

    我刚刚解决这个问题的方法是根本不使用XMLHTTPRequest,而是在单独的javascript文件中包含所需的数据 . (在我的情况下,我需要一个二进制SQLite blob与https://github.com/kripken/sql.js/一起使用)

    我创建了一个名为 base64_data.js 的文件(并使用 btoa() 转换我需要的数据并将其插入 <div> ,以便我可以复制它) .

    var base64_data = "U1FMaXRlIGZvcm1hdCAzAAQA ...<snip lots of data> AhEHwA==";
    

    然后像普通的javascript一样将数据包含在html中:

    <div id="test"></div>
    
    <script src="base64_data.js"></script>
    <script>
        data = atob(base64_data);
        var sqldb = new SQL.Database(data);
        // Database test code from the sql.js project
        var test = sqldb.exec("SELECT * FROM Genre");
        document.getElementById("test").textContent = JSON.stringify(test);
    </script>
    

    我想,将其修改为读取JSON,甚至是XML,都是微不足道的;我将把它作为读者的练习;)

相关问题