Callback on AJAX request doesn't fire
I've a MVC action method and I want to return a list of element in JSON format, to a callback function of a AJAX request. But when a send the response of a action method with a array of element on JSON, the callback function doesn't开发者_运维百科 fire.
This is an implementation of my action method :
public JsonResult ListAnexos(string idRelatorio)
{
EasyClinicEntities entities = new EasyClinicEntities();
IQueryable<EasywebAnexos> _list = from EasywebAnexos in entities.EasywebAnexos
where EasywebAnexos.IdRelatorios == idRelatorio
select EasywebAnexos;
return Json(new { List = _list.ToList().ToArray() }, JsonRequestBehavior.AllowGet);
}
my AJAX request code :
function makeRefreshAnexos(relatorio) {
$.ajax({
type: "GET",
url: "/Anexos/ListAnexos?idRelatorio="+relatorio,
success: function (jsonObject) {
alert(jsonObjct);
}
});
}
When I try to send a JSON object like the code above, the callback function works fine.
public JsonResult ListAnexos(string idRelatorio){
return Json(new {id="just for test"},JsonRequestBehavior.AllowGet);
}
I use the .net framework 4.0 and MVC 2.
I have run into an issue when using EF created classes as JSON return classes and the error is not immediately apparent. If the class has any navigation properties, The JSON serializer will fail due to a serialization loop. The easy way to fix this is to give your navigation properties internal getter accessors in the EF designer.
If you want an easy way to retrieve the error in Javascript code you can use this for quick and easy debugging:
function makeRefreshAnexos(relatorio) {
$.ajax({
type: "GET",
url: "/Anexos/ListAnexos?idRelatorio="+relatorio,
success: function (jsonObject) {
alert(jsonObjct);
}, error: function(errorData){
alert(errorData);
}
});}
精彩评论