XMLHttpRequest Origin null is not allowed Access-Control-Allow-Origin for file:/// to file:/// (Serverless)

前端 未结 9 776
醉梦人生
醉梦人生 2020-11-22 03:30

I\'m trying to create a website that can be downloaded and run locally by launching its index file.

All the files are local, no resources are used online.

Wh

相关标签:
9条回答
  • 2020-11-22 04:02

    The way I just worked around this is not to use XMLHTTPRequest at all, but include the data needed in a separate javascript file instead. (In my case I needed a binary SQLite blob to use with https://github.com/kripken/sql.js/)

    I created a file called base64_data.js (and used btoa() to convert the data that I needed and insert it into a <div> so I could copy it).

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

    and then included the data in the html like normal javascript:

    <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>
    

    I imagine it would be trivial to modify this to read JSON, maybe even XML; I'll leave that as an exercise for the reader ;)

    0 讨论(0)
  • 2020-11-22 04:03

    What about using the javascript FileReader function to open the local file, ie:

    <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>
    

    Now Click Choose file button and browse to the file file:///C:/path/to/XSL%20Website/data/home.xml

    0 讨论(0)
  • 2020-11-22 04:08

    For instances where running a local webserver is not an option, you can allow Chrome access to file:// files via a browser switch. After some digging, I found this discussion, which mentions a browser switch in opening post. Run your Chrome instance with:

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

    This may be acceptable for development environments, but little else. You certainly don't want this on all the time. This still appears to be an open issue (as of Jan 2011).

    See also: Problems with jQuery getJSON using local files in Chrome

    0 讨论(0)
  • 2020-11-22 04:08

    Launch chrome like so to bypass this restriction: open -a "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" --args --allow-file-access-from-files.

    Derived from Josh Lee's comment but I needed to specify the full path to Google Chrome so as to avoid having Google Chrome opening from my Windows partition (in Parallels).

    0 讨论(0)
  • 2020-11-22 04:11

    Here is an applescript that will launch Chrome with the --allow-file-access-from-files switch turned on, for OSX/Chrome devs out there:

    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 &"
    
    0 讨论(0)
  • 2020-11-22 04:13

    use the 'web server for chrome app'. (you actually have it on your pc, wether you know or not. just search it in cortana!). open it and click 'choose file' choose the folder with your file in it. do not actually select your file. select your files folder then click on the link(s) under the 'choose folder' button.

    if it doesnt take you to the file, then add the name of the file to the urs. like this:

       https://127.0.0.1:8887/fileName.txt
    

    link to web server for chrome: click me

    0 讨论(0)
提交回复
热议问题