尝试使用jQuery ajax和MVC显示JSONResult时出现内部服务器错误#500
调节器
public JsonResult TeamInfo(string teamName) { teamDA = new TeamDataAccess(); var teamInfo = teamDA.TeamInfo(teamName); System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer(); string sJSON = oSerializer.Serialize(teamInfo); JsonResult jsonResult =new JsonResult(){ JsonRequestBehavior = JsonRequestBehavior.AllowGet }; jsonResult.Data = sJSON; // first i give this. jsonResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet; return jsonResult; }
从jQuery调用Controller
$.ajax({ url: 'Team/TeamInfo/' + teamName, success: function (data) { $('#teamDetails').html(data); alert('Load was performed.'); $("#dialog-modal").dialog("open"); } });
在调试时我可以看到它正在执行,直到控制器的最后一行return jsonResult;
但alert('Load was performed.');
不可见。 你知道它不成功的部分原因是什么? 服务器端没有错误。 任何帮助都非常感谢。
编辑当我在ajax调用中添加error
时。 它说错误500(内部服务器错误)。 我如何找到这个问题?
您不需要使用JavaScriptSerializer进行所有序列化操作。
您的操作方法可能如下所示:
public JsonResult TeamInfo(string teamName) { return Json(new TeamDataAccess()TeamInfo(teamName), JsonRequestBehavior.AllowGet); }
$ .ajax的默认请求类型是GET
但默认情况下, JsonResult
不允许GET请求,因此您需要使用JsonRequestBehavior属性明确允许它们:
JsonResult jsonResult = new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet }; jsonResult.Data = sJSON; return jsonResult;
或者您可以使用type: 'POST'
使用ajax调用type: 'POST'
注意:目前还不清楚将返回的JSON直接放入带有$('#teamDetails').html(data);
行的DOM中的目标是什么$('#teamDetails').html(data);