0

我遇到了回调问题。我什至没有在 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();
    }

有任何想法吗?似乎这可能是相关的,如果不是抱歉,我可以发布一个新线程。

4

1 回答 1

1

尝试将动作签名中的返回结果设置为 aJsonResult而不是ActionResult.

    public JsonResult LoadMapLonLats(int mapId)
    {
        //some code
        return Json(_myMaps);
    }

进一步看这个,我怀疑这个问题可能与GETMVC 2 中对 JSON 结果调用的更改有关。

http://haacked.com/archive/2009/06/25/json-hijacking.aspx

基本上,您需要将呼叫更改为$.post()AcceptVerbs指定POST呼叫。

于 2010-02-27T12:46:47.990 回答