phonegap / phonegap-plugin-contentsync

Download and cache remotely hosted content
Apache License 2.0
206 stars 98 forks source link

Cross origin error #160

Closed tomasantunes closed 7 years ago

tomasantunes commented 7 years ago

I'm trying to read a downloaded json file but I get a cross origin error.

My code is:

sync.on('complete', function(data) {
  alert(data.localPath);

  $.getJSON("file://" + data.localPath + "/file.json", function(data) {
    alert('data: ' + JSON.stringify(data));
  });
});
macdonst commented 7 years ago

@tomasantunes I'm not a big user of jQuery but there is probably a parameter you can pass to allow cross origin or possibly you are using an old version of jQuery as it should allow cross origin to file:// paths.

You can just use regular AJAX code:

var request = new XMLHttpRequest();
    request.open("GET", "file://" + data.localPath + "/file.json", true);
    request.onreadystatechange = function() {//Call a function when the state changes.
        if (request.readyState == 4) {
            if (request.status == 200 || request.status == 0) {
                // returns a plain string
                alert('data: ' + data);
                // convert string data to an Object.
                var obj = JSON.parse(data);
            }
        }
    }
    request.send();
macdonst commented 7 years ago

Closing, no response in a month.