views:

324

answers:

1

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.

A: 

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.

Alastair Pitts