I'm geting a JSON response with an AJAX request through JavaScript.
Here is the response:
{"responseCode":400,"errors":false,"submitted":false,"content":"some content","notice":""}
My Goal is to get the content:
"some content"
The json variable is the data in my case. So, I have tried with:
data.content
But I am geting an empty string.
Any idea on how to access the string?
Thank you in advance.
Approach: To solve this problem, we will first consider a JSON file named “capitals. json” and try to get this JSON data as a response using AJAX. Then we will create an HTML file “capitals. html” which contains a table which we will use to populate the data we are getting in response.
Description: Takes a well-formed JSON string and returns the resulting JavaScript value.
Use the JavaScript function JSON.parse() to convert text into a JavaScript object: const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}'); Make sure the text is in JSON format, or else you will get a syntax error.
The jQuery. getJSON( url, [data], [callback] ) method loads JSON data from the server using a GET HTTP request. data − This optional parameter represents key/value pairs that will be sent to the server. callback − This optional parameter represents a function to be executed whenever the data is loaded successfully.
Did you first parse json ?
var data = JSON.parse(json);
than read data.content
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With