Guddyaaa
Guddyaaa

Reputation: 91

how to read parsed json data in ajax success call

On Ajax success call, I'm getting already parsed data in JSON format from a Controller. I want to read that data, so while I'm doing below one, I am getting undefined as an error. How can I solve this?

success : function(response) { 
    alert(response.operatorId); 
},

Upvotes: 2

Views: 1412

Answers (2)

lruchandani
lruchandani

Reputation: 1

It appears that the response coming into success function is not a JSON object. COuld you check if you have following set in your ajax call.

dataType: 'json',
contentType : 'application/json'

Alternatively, you may use the following to parse the json string to json object and then use dot notation to access the properties

success : function(response) { 
    var jsonData = $.parseJSON(response)
    alert(jsonData.operatorId); 
},

Upvotes: 0

Scary Wombat
Scary Wombat

Reputation: 44834

Here is an example of working code

 success: function(json) {
   console.log(JSON.stringify(json.topics));
   $.each(json.topics, function(idx, topic){
     $("#nav").html('<a href="' + topic.link_src + '">' + topic.link_text + "</a>");
   });
 }

Upvotes: 1

Related Questions