Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse JSON response got from AJAX request through JavaScript

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.

like image 721
Milos Cuculovic Avatar asked Jan 23 '13 11:01

Milos Cuculovic


People also ask

How do I get AJAX response in JSON format?

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.

What does the JSON parse () method do when we initiate an AJAX request?

Description: Takes a well-formed JSON string and returns the resulting JavaScript value.

How do you parse JSON in JavaScript?

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.

How JSON fetch data using AJAX?

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.


1 Answers

Did you first parse json ?

var data = JSON.parse(json);

than read data.content

like image 130
Dejo Avatar answered Sep 20 '22 15:09

Dejo