I am trying to return data from the server via a JsonResult in mvc. However for some strange reason I cannot pick up my code silently fails. The first alert on the client side succeeds and I do see the output “Got Here” but the second alert is never displayed. Why?
//Server Side
public JsonResult GetWeightsData()
{
PerfomanceMeasureDBDataContext db = new PerfomanceMeasureDBDataContext();
return this.Json(db.WeightMearsures.Select(x => new { Day =x.Date.ToString(), Weight = x.Weight }));
}
//Client Side
$(function () {
$('#ShowChart').click(function () {
alert("Got Here");
$.getJSON("/Home/GetWeightsData", null, function (data) {
alert(data[0].Day);
var dates = new Array();
var weights = new Array();
for (var i = 0; i < data.length; i++) {
dates[i] = data[i].Day;
weights[i] = data[i].Weight;
alert(dates[i]);
}
showChart(dates, weights);
});
});
});
HTTP GET requests are denied by default by JsonResult in MVC2+ so that could also be a problem with your code. Have you tried browsing the “/Home/GetWeightsData” action directly from a browser?
Try:
The reason for it is here
http://msdn.microsoft.com/en-us/library/system.web.mvc.jsonrequestbehavior%28VS.100%29.aspx