Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a situation where a when a web page is accessed a controller action runs which retrieves the data for that page based on a user selection. I am attempting to send the data back to the page as a JSON object, however, the data opens up as one large string in an HTML page. The controller action, in a nutshell, looks like the following snippet:

Public JsonResult MyMethod(string userSelection)
{

    string userData = (string) Data;
    return Json(userData, “text”, JsonRequestBehavior.AllowGet);
}

I first tried to use the JQuery $.getJson() method but I think this is wrong as I believe it issues another call to the action method for the data, which is not what I want to do. What I want is to access the JSON object in JavaScript code so I can use the property data to populate fields on the web page. The basic question is what must I do in my JavaScript to receive the JSON object when the page is first rendered? I apologize if I am missing something fundamental; this is my first try.

share|improve this question
Please post the requested URL. – asawyer May 25 '12 at 18:37

3 Answers

You want parseJSON not getJSON

http://api.jquery.com/jQuery.parseJSON/

Edit - Oh wait you are pointing your browser at the JsonResult as if it was an ActionResult? That is not going to work.

Render a proper view, and use getJSON to call the JsonResult action.

share|improve this answer
To return JSON from action, JSONResult will work same as ActionResult. JSonResult is inherited from ActionResult. – Shyju May 25 '12 at 18:33
@Shyju Right, I think, but am not sure, the OP is saying his route is pointed at the JsonResult action, explaining the json string all over the page. – asawyer May 25 '12 at 18:34
I think it is a problem somewhere in how he handle the data at client side. – Shyju May 25 '12 at 18:35
@Shyju That's possible too. We need to see the route url to eliminate it as an option. – asawyer May 25 '12 at 18:37

getJSON is what you are looking for. Call that on the DOM ready event which will executes once the DOM finishes loading.

$(function(){
 //This code will be executed on the DOM ready ( when the page is loaded)

  $.getJSON("YourControllerName/MyMethod?userSelection=someValue",function(data){

      alert(data.FirstName);     
      alert(data.AnotherPropertyName);    

   });
});

getJSON is a shorthand of jQuery ajax method with datatype set as json

Assuming your JSON data you are retuning is something like this

{
    "FirstName": "Scott",
    "AnotherPropertyName": "SomeValue"
}

To return data like above, change your Action method like this

public JsonResult MyMethod(string userSelection)
{
  var result=new { FirstName="Scott", AnotherPropertyName="Great"};
  return Json(result,JsonRequestBehavior.AllowGet);
}
share|improve this answer
You are using a Json overload I am unfamiliar with, the only overload I see that accepts a string as the second parameter sets the response MIME type... ? – asawyer May 25 '12 at 18:25
@asawyer :Ah ! I copied a wrong thing from clipboard. Thanks for pointing. Corrected it. – Shyju May 25 '12 at 18:29

I still had no luck today but when I left work I came up with a strategy walking to my car. A user makes a selection from a page that presents a list prior to entering the page on which I cannot figure out how to work with JsonResult. Part of the problem is the user selection contains a link that calls the controller/action that returns the JsonResult which conflicts with using $.getJson() within the page where I want to work with JsonResult. So here is my strategy: When the user makes the selection that brings them to the (problematic) page, I will call a controller/action that strictly works with (ASP) ViewData, and use the ViewData to initially present that page. Once on the page, the user can change the selection -- I will handle this with a JavaScript event that uses a $.getJason() call to a different controller/action method that works with (ASP) JsonResult. After I try this strategy I shall post my results for whomever is interested.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.