EagleFox
EagleFox

Reputation: 1367

How to get a value from Json result

This is the json result I get from my controller

{"data":"Sunday"}

The data can say any day of the week (Sunday, Monday, etc...)

On success I want to do this in ajax call

success: function(Response){
        var myresponse = Response.data;
        alert(myresponse);
}

However, it gives me undefined.

Upvotes: 0

Views: 217

Answers (4)

Abdel Raoof Olakara
Abdel Raoof Olakara

Reputation: 19353

If you are sure that you are getting a JSON response from the server, you can make use of the Ext.JSON class to decode the JSON.

You can use the decode() method to convert a string to an object. Then you should be able to easily access it.

Example:

var jsonObject = Ext.JSON.decode(Response.responseText);
var myData = jsonObjet.data; 

Upvotes: 1

BuddhiP
BuddhiP

Reputation: 6451

If you are using jQuery to load this string you could just use $.getJSON which will automatically parse the string and pass the object as the return value to the 'success' function.

Upvotes: 1

ek_ny
ek_ny

Reputation: 10233

It might be considering your response to be a string. I would do something like this:

success: function(Response){
        alert(typeof Response);
        var myresponse = Response.data;
        alert(myresponse);
}

If it tells you that Response is string, you need to make sure that your framework knows you are getting back JSON. For instance with jquery it might be $.getJSON().

Upvotes: 0

axel
axel

Reputation: 4127

try to use a

console.log(Response);

to check the content of Response

Upvotes: 0

Related Questions