marcusstarnes
marcusstarnes

Reputation: 6531

Retrieve values from getJSON() call in jQuery

I have an ajax call in my jquery to my MVC controller:

$.getJSON('/mysite/controller/dosomething', { postId: id }, function (data) {

The 'data' that I am returning is in the form of a JsonResult, and consists of a simple custom object with 1 property called 'Message' and another property called 'Count'. Both of these values are assigned and I am returning them as follows (edited for brevity):

[HttpGet]
public JsonResult DoSomething(int postId)
{
    var response = new MyAjaxResponseModel {Message = "Hello world!", Count = 66};
    return Json(response, JsonRequestBehavior.AllowGet);
}

In my jQuery, I then want to be able to look at both values in the Json response, but I don't know the proper way to get at these values?

Upvotes: 0

Views: 50

Answers (1)

shesek
shesek

Reputation: 4682

data.Message, data.Count in the callback you're passing to $.getJSON()? To inspect the structure of your data object, you can use console.log(data) (also, in that callback)

Upvotes: 1

Related Questions