I ma making a simple post request to my controller which is returning a json object that I am trying to parse, that is not working.
public ActionResult GetViewEditData(int machineID)
{
ViewBag.MachineData = Json(PMIComputerModel.GetViewEditDetails(machineID));
return View("GetViewEditData");
}
Ajax call
function GetDetails(crntAnc, mode) {
var id = $(crntAnc).parents('tr:first').find('td:first').attr('id');
alert(id);
$.ajax({
url: '/Home/GetViewEditData/',
contentType: "application/json; charset=utf-8",
type: 'POST',
data: "{'machineID': '"+ id +"'}",
success: function () {
},
error: function (e, jqxhr, settings, exception) {
if (jqxhr != null)
alert(jqxhr.responseText);
}
});
var data = '@ViewBag.MachineData';
alert(data);
alert(jQuery.parseJSON(data));
}
In the error block , it is coming as undefined. All i want do is parse my josn data in my javascript.
The class that is being serialized :
public class PMICheckListWrapper
{
public PMIMachine Machine { get; set; }
public List<PMICheckListResponse> CheckResponseList { get; set; }
}
PS: Edit: Since I was not accessing in success function and also not returning any thing I have taken the pasring logic outside the ajax post but still it is coming
var data = '@ViewBag.MachineData';
alert(data); // blank
alert(jQuery.parseJSON(data)); //null