-1

I wrote script, that gets data from json file. It works correct in Firefox, but in Chrome is error:

OPTIONS file:///home/[...]/tilemaps/testmap.json Origin null is not allowed by Access-Control-Allow-Origin. jquery.js:6 XMLHttpRequest cannot load file:///home/[...]/tilemaps/testmap.json. Origin null is not allowed by Access-Control-Allow-Origin.

This is code:

function newTileMapFromJSON(src) {
    var mymap;
    $.getJSON(src, function(data) {
        mymap = data;
        //console.log(mymap);
        console.log("Mapa 0, 0 : " + mymap.layers[0].data[0]);
        var wOfArray = mymap.layers[0].width;
        var hOfArray = mymap.layers[0].height;
        var mapArr = [];
        var imAt = 2;
        for (var x = 0; x < wOfArray; x++) {
            for (var y = 0; y < hOfArray; y++) {
                console.log("imAt" + imAt + " x: " + x);
                mapArr[x][y] = mymap.layers[0].data[imAt];
                imAt++;
            }
        }
        console.log("mapArr: " + mapArr);
    });
}

newTileMapFromJSON("tilemaps/testmap.json");

Why it doesn't work? Why it works on Firefox but not on Chrome. PS: In Firefox, for loop works only once. But it should work 25x25 times :/

Ian
  • 50,146
  • 13
  • 101
  • 111
Piotrek
  • 10,919
  • 18
  • 73
  • 136

1 Answers1

2

file:/// is considered inherently dangerous. If you want chrome to allow XHR retrievals when running from file rather than from a localhost server (python -m SimpleHTTPServer, etc) then you're going to have to start Chrome with the flag --allow-file-access-from-files

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153