我遇到了回调问题。我甚至没有在Firebug中收到错误。如果我在getjson调用之前和之后发出警报,则两个警报都显示但getjson调用不会触发。
public ActionResult TestPage()
{
return View();
}
public ActionResult LoadMapLonLats(int mapId)
{
//some code
return Json(_myMaps);
}
$("#Search").click(function() {
$.getJSON("LoadMapLonLats", { mapId: 73 }, loadDbMap);
});
function loadDbMap(maps) {
alert('m');
$.each(maps, function(i) {
alert(maps[i]);
});
}
只要我没有参数就离开TestPage就行了。如果我向TestPage(int id)添加一个参数,那么回调到LoadMapLonLats的方法不起作用。看起来很奇怪。当然TestPage是我正在加载的页面,所以我需要在渲染页面之前做一些工作。不确定为什么在视图中添加参数会破坏对另一个函数的回调。
//this breaks he callback to LoadMapLonLats
public ActionResult TestPage(int id)
{
return View();
}
有什么想法吗?似乎这可能是相关的,如果不是抱歉,我可以发布新的帖子。
答案 0 :(得分:1)
尝试将操作签名中的返回结果设置为JsonResult
,而不是ActionResult
。
public JsonResult LoadMapLonLats(int mapId)
{
//some code
return Json(_myMaps);
}
进一步研究一下,我怀疑这个问题可能与MVC 2中对JSON结果的GET
调用有关。
http://haacked.com/archive/2009/06/25/json-hijacking.aspx
基本上,您需要将通话更改为$.post()
并让AcceptVerbs
指定POST
来电。