Jump to content

json


etsted

Recommended Posts

any reason to why json wont work localy or on my server?

 

i am trying this code:

 

// this is my json_script.htm

 

<!DOCTYPE html><html><head><script type="text/javascript">function ajax_get_json(){var results = document.getElementById("results"); var hr = new XMLHttpRequest(); hr.open("GET", "mylist.json", true); hr.setRequestHeader("Content-type", "application/json", true); hr.onreadystatechange = function() { if(hr.readyState == 4 && hr.status == 200) { var data = JSON.parse(hr.responseText); results.innerHTML = ""; var results = document.getElementById("results"); results.innerHTML = data.user; } } hr.send(null); results.innerHTML = "requesting...";}</script></head><body><div id="results"></div><script type="text/javascript">ajax_get_json();</script></body></html>

 

 

// this is my mylist.json

 

{ "user":"John", "age":22, "country":"United States" };

Link to comment
Share on other sites

how is it not working? Are you checking for errors? Looking at the networks tab to inspect the request / response? you should add console.log statements to your code so you can trace the execution.

Link to comment
Share on other sites

results.innerHTML = "requesting..."; that part is working, but it does not get the user content from my mylist.json

 

how is it not working? Are you checking for errors? Looking at the networks tab to inspect the request / response? you should add console.log statements to your code so you can trace the execution.

Link to comment
Share on other sites

what do you mean "networks tab to inspect the request / response?"

most modern browsers have developer tools so you can see what your code is doing. They include things like a DOM inspector, error console, networks tab, etc.

 

here is chrome's, for example

https://developers.google.com/chrome-developer-tools/

 

That way you actively debug your application, instead of trying to guess.

 

 

how i can i allow the console.log to work?

https://developer.mozilla.org/en-US/docs/Web/API/console.log

 

 hr.onreadystatechange = function() {  console.log(' inside hr.onreadstatechange'); }
Link to comment
Share on other sites

Make sure your JSON syntax is correct. All strings and property names must be delimited by double-quotes:

 

{ "property" : "value" }

 

The following are incorrect:

{ "property" : 'value' }

{ property : "value" }

{ 'property' : "value" }

Link to comment
Share on other sites

you can use this site to validate your JSON

http://jsonlint.com/

 

if that semicolon at the end is actually in your JSON

 

{ "user":"John", "age":22, "country":"United States" };

 

then that's invalid

Link to comment
Share on other sites

i validted the json code, and it had no errors, but i still get 4 errors when i run the code.

 

TypeError: vendor is undefined hotels.php:1174

The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range.

The character encoding of the page must be declared in the document or in the transfer protocol. json_tutorial.htmlTypeError: results is undefined json_tutorial.html:19TypeError: vendor is undefined

 

 

my json_script.htm

 

<!DOCTYPE html><html><head><script type="text/javascript">function ajax_get_json() {var results = document.getElementById("results"); var hr = new XMLHttpRequest(); hr.open("GET", "mylist.json", true); hr.setRequestHeader("Content-type", "application/json", true); hr.onreadystatechange = function() { if(hr.readyState == 4 && hr.status == 200) { var data = JSON.parse(hr.responseText); var results = document.getElementById(data); console.log(data); results.innerHTML = data.user; } } hr.send(); results.innerHTML = "requesting...";}</script></head><body><div id="results"></div><script type="text/javascript">ajax_get_json();</script></body></html>

 

 

my mylist.json

 

{ "user":"John", "age":"22", "country":"United States" }

Link to comment
Share on other sites

var results = document.getElementById(data);Why are you trying to use a JSON structure as an ID?I'm not sure if those error messages go with that code. You're not using something called "vendor".

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...