Reputation: 2804
i am new with JSON. I am using a api which using JSON for response. I am calling the api url by simple xhr method. The code is working and i am getting 200 status but
how do i able to retrieve the JSON in the html?
I was able to show the JSON by printing xhr.responseText in innerHTML. But i need to show the data so i can use them in html.
Can you suggest how i can present the data in html when i have the json data in xhr.responseText.
Please let me know if you need more info.
Can anyone guide me how to do it via jquery also?
Upvotes: 1
Views: 1232
Reputation: 2804
here is another post which works and less code because of jquery. The first answer works perfectly. I thought this might be help for people who having same problem.
Trying to use jQuery to display JSON text data
Upvotes: 0
Reputation: 4394
If you add JSON2.js (from https://github.com/douglascrockford/JSON-js/blob/master/json2.js) to your page, then you can parse the JSON into a JavaScript object by using:
var parsedData = JSON.parse(xhr.responseText);
If you completely trust your JSON source, you can also do it very quickly and without using JSON2.js by simply evaling it thus:
var parsedData = eval('(' + xhr.responseText + ')');
but this will run any code embedded in the response and so isn't secure and generally wouldn't be recommended.
Upvotes: 1