My issue arises when a web page is loaded and triggers a controller action to retrieve data based on user selection. I am trying to return this data as a JSON object, but it appears as a single string within the HTML page. The basic structure of the controller action looks like this:
Public JsonResult MyMethod(string userSelection)
{
string userData = (string) Data;
return Json(userData, “text”, JsonRequestBehavior.AllowGet);
}
Initially, I attempted to use JQuery's $.getJson()
method, but realized that it makes another request to the action method for data, which is not what I intended. What I actually need is to access the JSON object in JavaScript code so that I can utilize the data property to populate fields on the web page. So, my question boils down to what steps should be taken in my JavaScript to receive the JSON object upon the initial rendering of the page? I'm new at this, so please forgive any oversights on my part.