How to get a data.json
{"key1":"val1","key2":"val2"}
with a function
var getJSON = function (url) {
var response = null;
return (function () {
var xhr = new XMLHttpRequest();
xhr.open('get', url, true);
xhr.responseType = 'json';
xhr.onload = function () {
response = xhr.status == 200 ? xhr.response : xhr.status;
};
xhr.send();
})();
return response;
};
That the code
console.log(1);
console.log(getJSON('http://localhost/myproject/data.json'));
console.log(3);
gives
1
{"key1":"val1","key2":"val2"}
3
? Now it gives
1
null
3
Thank you