开发者

getJSON not working if the mvc model view controller has a parameter

I'm having an issue with a callback. I'm not even getting an error in Firebug. If I alert before and after the getjson call both alerts show but the getjson call doesn't fire.

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]);
        });
    }

As long as I leave TestPage without a parameter is works. If I add a parameter to TestPage(int id) then the call back to LoadMapLonLats doesn't work. Seems odd. Of course TestPage is the page I'm loading so I need to do some work here before rendering the page. Not sure why adding a parameter to the view would break the callback to another function.

//this breaks he callback to LoadMapLonLats

public ActionResult TestPage(int id)
    {

       开发者_运维技巧 return View();
    }

Any ideas? Seems like this may be related, if not sorry I can post a new thread.


try setting the return result in the action signature as a JsonResult instead of an ActionResult.

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

Having a further look at this, I suspect the issue could be related to the changes regarding GET calls to a JSON result in MVC 2.

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

Basically you need to change the call to $.post() and have the AcceptVerbs specify a POST call.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜