$.getJSON not returning a response with MVC3
I spent a LONG time looking into this and just can't see what's wrong. I have the following:
$.getJSON(
'/adminStatus/开发者_JS百科GetJsonData',
{ name: $('#textSearch')[0].value },
function (data) {
alert("3");
// $('#studentList > div').remove();
// for (s in data) {
// alert("4");
// var student = data[s];
// $('#studentList').append('<div>(' + student.StudentId + ') ' + student.FirstName + ' ' + student.LastName + '</div>');
// }
}
);
This code fires an action in my controller and that action returns data. It's actually code from another example:
public JsonResult GetJsonData(string name)
{
return new JsonResult
{
Data = (from student in Student.GetStudentDataList()
where student.LastName.StartsWith(name)
select student).ToArray<Student>()
};
}
I check and Data is getting populated with data.
However nothing is happening with return data and when I added comments then I cannot even get the alert("3") to appear.
Am I doing something really obviously wrong? I think I'm copying a working example but nothing seems to be returned and the function (data) {} does not seem to execute.
Any help would be much appreciated.
Thanks,
It would be best to check this in something like Firebug to see if there are any errors occurring. Trying to troubleshoot without knowing exactly what's going on is just going to lead to more frustration.
One thing I see being a potential problem is that you don't have JsonRequestBehavior.AllowGet
set, so you might be getting a server error. To see if that's the case you can change your return to read:
var data = (from student in Student.GetStudentDataList()
where student.LastName.StartsWith(name)
select student).ToArray<Student>();
return Json(data, JsonRequestBehavior.AllowGet);
When debugging this kind of problem , fiddler is the best tool since it's always showing you what's under the hood.
精彩评论