You can use jQuery .getJSON() function:
$.getJSON('http://query.yahooapis.com/v1/public/yql?q=select%20%2a%20from%20yahoo.finance.quotes%20WHERE%20symbol%3D%27WRC%27&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys&callback', function(data) {
// JSON result in `data` variable
});
If you don't want to use jQuery you should look at this answer for pure JS solution.
Answer from Dan Barzilay on Stack OverflowYou can use jQuery .getJSON() function:
$.getJSON('http://query.yahooapis.com/v1/public/yql?q=select%20%2a%20from%20yahoo.finance.quotes%20WHERE%20symbol%3D%27WRC%27&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys&callback', function(data) {
// JSON result in `data` variable
});
If you don't want to use jQuery you should look at this answer for pure JS solution.
If you want to do it in plain javascript, you can define a function like this:
var getJSON = function(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status === 200) {
callback(null, xhr.response);
} else {
callback(status, xhr.response);
}
};
xhr.send();
};
And use it like this:
getJSON('http://query.yahooapis.com/v1/public/yql?q=select%20%2a%20from%20yahoo.finance.quotes%20WHERE%20symbol%3D%27WRC%27&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys&callback',
function(err, data) {
if (err !== null) {
alert('Something went wrong: ' + err);
} else {
alert('Your query count: ' + data.query.count);
}
});
Note that data is an object, so you can access its attributes without having to parse it.
Videos
For reading the external Local JSON file (data.json) using javascript, first create your data.json file:
data = '[{"name" : "Ashwin", "age" : "20"},{"name" : "Abhinandan", "age" : "20"}]';
Then,
Mention the path of the json file in the script source along with the javascript file
<script type="text/javascript" src="data.json"></script> <script type="text/javascript" src="javascript.js"></script>Get the Object from the json file
var mydata = JSON.parse(data); alert(mydata[0].name); alert(mydata[0].age); alert(mydata[1].name); alert(mydata[1].age);
The loading of a .json file from a hard disk is an asynchronous operation, and thus it needs to specify a callback function to execute after the file is loaded.
function readTextFile(file, callback) {
var rawFile = new XMLHttpRequest();
rawFile.overrideMimeType("application/json");
rawFile.open("GET", file, true);
rawFile.onreadystatechange = function() {
if (rawFile.readyState === 4 && rawFile.status == "200") {
callback(rawFile.responseText);
}
}
rawFile.send(null);
}
//usage:
readTextFile("/Users/Documents/workspace/test.json", function(text){
var data = JSON.parse(text);
console.log(data);
});
This function also works for loading a .html or .txt files, by overriding the mime type parameter to "text/html", "text/plain", etc.
After so many months of search, I found the solution. Hence, I am answering my own question.
When JSON is not supported and when we are stuck with Same Origin Policy, we have to wrap around our JSON with a padding and make it a JSONP.
To do that, we have a life saving website http://anyorigin.com/
You can paste your URL and get the corresponding JQuery code something like this,
$.getJSON('http://anyorigin.com/get?url=http%3A//webapp.armadealo.com/home.json&callback=?', function(data){
$('#output').html(data.contents);
});
If you want to use your own code, then just use the URL from the code above, which is
http://anyorigin.com/get?url=http%3A//webapp.armadealo.com/home.json&callback=?
This above URL will give you the same JSON data as JSONP and solves all the trouble.
I had used the following code, which on success calls displayAll function
$.ajax({
url: 'http://anyorigin.com/get?url=http%3A//webapp.armadealo.com/home.json&callback=?',
type: 'GET',
dataType: "json",
success: displayAll
});
function displayAll(data){
alert(data);
}
If you look in Chrome inspector, you probably see this error:
XMLHttpRequest cannot load http://webapp.armadealo.com/home.json. Origin http://stackoverflow.com is not allowed by Access-Control-Allow-Origin.
What this means is that the server doesn't want the client web page reading the file. The client isn't trusted. This is a basic security feature of XMLHttpRequest in order to prevent a site like mybank.evil.com from downloading data from mybank.com. It unfortunately makes testing from a local file challenging.
If you trust any site with your data or a select number of sites, you can configure your server script to send the Access-Control-Allow-Origin to allow certain sites through.
See https://developer.mozilla.org/en/http_access_control for more details.
I'm working on a platformer game in HTML, and have the level data stored in a JSON file. It's right there, in the same folder, and yet I can't find a simple way to access it.
I've seen 2 approaches online, one is to put the entire thing in quotes, then add "data=" to the start, and run it like a JS file. This is beyond stupid, but I'll settle for it if I have to.
The other involves making an actual "server" request to my local machine, but when I try that I get an error in chrome, saying that CORS doesn't allow you to access files using request type null. Other people in the comments were saying it was a bad idea to do that.
Is there seriously no easy way to just say "load this file", without jQuery or node?
$.ajax({
url: "\countries.json",
success: function (data) {
var obj = JSON.parse(data);
}
});
For safety reasons, The C:\Users\My Documents\new\WebContent\JsonFiles\TreeJson\countries.json url won't work in a browser.
Please try to see this example.
Jquery to get Json
-or-
How to use a JSON file in javascript
Hope may help you.