charlie_cat
charlie_cat

Reputation: 1850

How to see what JSON returns in my view

Can some one help or explain to me how this works please? i have in my view:

 <script type="text/javascript">
   var jqxhr = $.getJSON("<%= Url.Action("GetTrainingModulePoints" , "Home") %>", function (json) {
    console.log(json);
    });
 </script> 

GetTrainingModulePoints should return 4 rows each containing a value for interactiontype and a value for points

in the console log i just get [object object]? how can i see what is in the variable json?

http://imageshack.us/photo/my-images/542/firebug.png/

thanks

Upvotes: 1

Views: 64

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038810

Look at the Network tab in FireBug or similar javascript debugging tool. There you will see the AJAX request and all you have to do is expand the request and look at the response.

Here's for example how that might look like in FireBug for a sample AJAX request:

enter image description here

And if you click on the JSON tab you will see the output formatted as a JSON object where you could expand/collapse the properties.

If for some very weird reason you cannot use a javascript debugging tool in your web browser (I don't know even know how you could be developing a web application, but ...) you could use the JSON.stringify method that's built into modern browsers:

var jqxhr = $.getJSON("<%= Url.Action("GetTrainingModulePoints" , "Home") %>", function (json) {
    alert(JSON.stringify(json));
});

And if you are not using a modern browser which doesn't have the JSON.stringify method natively built-in you could still reference the json2.js script to your page.


UPDATE:

OK, it seems that your confusion comes from the fact that you are getting {"success":true} whereas you were expecting to get the rows from your stored procedure. I know this because I answered your previous question.

Here's how your controller action look like:

[HttpGet]
public JsonResult GetTrainingModulePoints()
{
    var currentUser = ZincService.GetUserForId(CurrentUser.UserId);
    ZincService.TrainingService.GetTrainingModulePoints(currentUser.UserId);
    return Json(new { success = true }, JsonRequestBehavior.AllowGet);
}

As you can see in this controller action you are always returning success = true. If you want to return the results from your stored procedure you could do this:

[HttpGet]
public JsonResult GetTrainingModulePoints()
{
    var currentUser = ZincService.GetUserForId(CurrentUser.UserId);
    var modulePoints = ZincService.TrainingService.GetTrainingModulePoints(currentUser.UserId);
    return Json(
        new { 
            success = true, 
            modulePoints = modulePoints 
        }, 
        JsonRequestBehavior.AllowGet
    );
}

Here I assume that your TrainingService.GetTrainingModulePoints method is actually returning some object. If this is not the case you will have to explain what those methods are doing and how do you expect to get the output.

Upvotes: 2

Related Questions