【问题标题】:getJSON not working if the mvc model view controller has a parameter如果 mvc 模型视图控制器有参数,getJSON 不起作用
【发布时间】:2011-01-21 18:49:46
【问题描述】:

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

有什么想法吗?看来这可能是相关的,如果不是很抱歉,我可以发布一个新线程。

【问题讨论】:

  • 使用 firebug 并检查 net 选项卡。您可能会在那里发现问题。

标签: jquery asp.net-mvc getjson


【解决方案1】:

尝试将操作签名中的返回结果设置为JsonResult,而不是ActionResult

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

进一步研究后,我怀疑这个问题可能与 GET 调用 MVC 2 中的 JSON 结果有关。

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

基本上,您需要将调用更改为$.post(),并让AcceptVerbs 指定POST 调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-27
    • 2011-07-16
    • 1970-01-01
    • 2010-12-05
    • 1970-01-01
    • 2015-01-19
    • 1970-01-01
    • 2016-01-26
    相关资源
    最近更新 更多