khushboo.singh Posted June 17, 2014 Share Posted June 17, 2014 How to add .json file in html document. I tried like this ,but not got output array.json file var employees = [ { "firstName":"John", "lastName":"Doe" }, { "firstName":"Anna", "lastName":"Smith" }, { "firstName":"Peter", "lastName":"Jones" }, ]; employees[0].firstName="Gilbert"; document.getElementById("demo").innerHTML = employees[0].firstName + " " + employees[0].lastName; abc.html file <!DOCTYPE html> <html> <head> <script src="array.json"></script> </head> <body> <h2>Create a JavaScript Object</h2> <p id="demo"></p> </body> </html> Link to comment Share on other sites More sharing options...
justsomeguy Posted June 17, 2014 Share Posted June 17, 2014 What happens when you run that page? Are you checking for errors in your developer console? There should be at least one error message there. Link to comment Share on other sites More sharing options...
khushboo.singh Posted June 18, 2014 Author Share Posted June 18, 2014 No error message but, but when rendering html file not adding json file Link to comment Share on other sites More sharing options...
thescientist Posted June 18, 2014 Share Posted June 18, 2014 are you mixing .json and JS in one file? I don't think that's going to work. I would just put it all in one JS file and run this employees[0].firstName="Gilbert";document.getElementById("demo").innerHTML = employees[0].firstName + " " + employees[0].lastName; Link to comment Share on other sites More sharing options...
justsomeguy Posted June 18, 2014 Share Posted June 18, 2014 No error message but, but when rendering html file not adding json fileIs the path correct? You can check on the network tab in the developer tools to make sure it is requesting and finding that file. As you showed in the first post, that code will produce a fatal runtime error. I don't think it's necessary, but you might want to add a type attribute to the script tag also. I think that Javascript is the default though, so that shouldn't be necessary. It might be necessary if your server does not send a Javascript content type header for a .json file though. Link to comment Share on other sites More sharing options...
khushboo.singh Posted June 20, 2014 Author Share Posted June 20, 2014 Got out puting. In html file put <script src="array.json"></script> in body tag instead of head tag. Then it will work you try it. Thanks everyone Link to comment Share on other sites More sharing options...
davej Posted June 20, 2014 Share Posted June 20, 2014 You have an extra comma at the end of the array. 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