Skip to content

Instantly share code, notes, and snippets.

@rudin
Created October 30, 2015 10:54
Show Gist options
  • Save rudin/0088021177007e2216ef to your computer and use it in GitHub Desktop.
Save rudin/0088021177007e2216ef to your computer and use it in GitHub Desktop.
Load a local json in your Cordova app
Add to app .plst
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
var getJSON = function(url) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('get', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status == 200 || status == 0) {
resolve(xhr.response);
} else {
reject(status);
}
};
xhr.send();
});
};
loadData = function (callbackFN) {
getJSON('assets/data.json').then(function(data) {
callbackFN(data);
}, function(status) {
console.log('Something went wrong.');
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment