0

I can't seem to get JSON when it's from an external file. When I write it inline, it works fine. But when I created a file called test.json and copied the JSON in to it, I never get the contents.

Here's my HTML and JavaScript. I should note that both HTML and JSON files are within the same folder.

What am I doing wrong?

<!DOCTYPE html> <html> <head> <title>JSON Sandbox</title> </head> <body> <h2>JSON Sandbox</h2> <p id="demo"></p> <script type="text/javascript"> var text = $.getJSON({ dataType : "json", url : "test.json", data : data, success : window.alert("JSON Aquired.") }); var obj = JSON.parse(text); document.getElementById("demo").innerHTML = obj.name + "<br>" + obj.street + "<br>" + obj.phone; </script> </body> </html> 

Here's my test.json file

{ "name":"John Johnson", "street":"Oslo West 1", "phone":"111 1234567" } 
3
  • Try and check : Create a *.js file inspite of *.json.. Commented Sep 16, 2014 at 14:29
  • Don't edit the answer into your question, please. Commented Sep 16, 2014 at 14:32
  • That didn't do it. Thanks. Commented Sep 16, 2014 at 14:33

2 Answers 2

2

Change the file extension to js. and change the html file as below:

<!DOCTYPE html> <html> <head> <title>JSON Sandbox</title> <script src="jquery-1.8.2.min.js"></script> </head> <body> <h2>JSON Sandbox</h2> <p id="demo"></p> <script type="text/javascript"> var obj = new Object(); var error = new Object(); $.getJSON('test.js').done(function (data) { obj = data; document.getElementById("demo").innerHTML = obj.name + "<br>" + obj.street + "<br>" + obj.phone; }).error(function (err) { error = err; }); </script> </body> </html> 
Sign up to request clarification or add additional context in comments.

3 Comments

Still not working, you can check it out at dev.tessierit.com/json/index.html
try downloading the googleapis js file to local folder and then referencing it as <script src="jquery-1.8.2.min.js"></script>
There is absolutely no reason anything suggested in this answer after the code should have made a difference. I'm not sure what happened, but kudos, I guess.
0

Your success handler is defined incorrectly.

Replace:

success : window.alert("JSON Aquired.") 

With:

success : function(data){ window.alert("JSON Aquired.") // `data` is the returned object: document.getElementById("demo").innerHTML = data.name + "<br>" + data.street + "<br>" + data.phone; } 

You need to do what you want to do with the data, in the success handler, because $.getJSON is an AJAX call, which means it's asynchronous.

9 Comments

You'll also have to move the code that uses the data into the success handler.
@RyanKinal: just noticed that, adding it to my answer.
@Frantumn: Are you getting any errors? What do you see when you console.log(data) in the success handler?
Uncaught ReferenceError: $ is not defined this is on the var text line.
Make sure you've included jQuery properly.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.