etsted Posted April 30, 2014 Share Posted April 30, 2014 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 More sharing options...
thescientist Posted April 30, 2014 Share Posted April 30, 2014 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 More sharing options...
etsted Posted April 30, 2014 Author Share Posted April 30, 2014 results.innerHTML = "requesting..."; that part is working, but it does not get the user content from my mylist.json Link to comment Share on other sites More sharing options...
thescientist Posted April 30, 2014 Share Posted April 30, 2014 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 More sharing options...
etsted Posted April 30, 2014 Author Share Posted April 30, 2014 what do you mean "networks tab to inspect the request / response?" how i can i allow the console.log to work? Link to comment Share on other sites More sharing options...
thescientist Posted April 30, 2014 Share Posted April 30, 2014 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 More sharing options...
etsted Posted April 30, 2014 Author Share Posted April 30, 2014 it says it is expecting SyntaxError: JSON.parse: expected property name or '}' Link to comment Share on other sites More sharing options...
Ingolme Posted April 30, 2014 Share Posted April 30, 2014 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 More sharing options...
thescientist Posted April 30, 2014 Share Posted April 30, 2014 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 More sharing options...
davej Posted April 30, 2014 Share Posted April 30, 2014 Wow, I didn't realize JSON didn't like single quotes. I don't think Mozilla JSON.stringify() cares. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 30, 2014 Share Posted April 30, 2014 Here's the official format, a string is defined as being between double quotes:http://json.org Link to comment Share on other sites More sharing options...
etsted Posted May 1, 2014 Author Share Posted May 1, 2014 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 More sharing options...
justsomeguy Posted May 1, 2014 Share Posted May 1, 2014 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 More sharing options...
etsted Posted May 2, 2014 Author Share Posted May 2, 2014 I fixed it as usual Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now