【问题标题】:Jquery postback not working on mvc 4 client sideJquery postback 在 mvc 4 客户端上不起作用
【发布时间】:2012-10-28 12:25:41
【问题描述】:

我正在尝试简单地创建一个 json 回发,以便我可以更新客户端的一些控件。我找不到一个很好的例子来证明这一点。

这是我到目前为止所得到的,它似乎正在从控制器发出警报,但在客户端一直说“未定义”对象。

什么是实现这一目标的最佳实践方法,因为我不知道如何以与常规代码相同的方式调试 javascript? :( 我正在使用 vs2012 express、mvc 4、jquery 1.7.1 和 jquery mobile 1.1。

我的控制器时间/索引:

[HttpPost]
public JsonResult Index()
{
     var msg = "hello there";  //test message
     return Json(msg);
}

我的客户端:

function populateUserDetails() {
     var user = {};
     user.UserId = $("#UserId").val();    // potential fields i may use once i get it working
     $.post('Time/Index', user, updateFields, 'json');
 };

updateFields = function (data) {
    alert("hi " + data.msg);
    $("#textEntered").val(data.msg);
};

*** 更新 ***** ****

通过将控制器中返回的对象包装到一个临时类中来修复它:

    [HttpPost]
    public JsonResult Index()
    {
        var response = new {msg = "hello there"};   //here's what i changed
        return Json(response);
    }

【问题讨论】:

    标签: asp.net-mvc json jquery asp.net-mvc-4 postback


    【解决方案1】:

    如下替换最后一行代码。

    return Json(response,JsonRequestBehavior.AllowGet);

    应该是这样的

    [HttpPost]

    public JsonResult Index()
    {
        var response = new {msg = "hello there"};  
       //here's what i changed
    
       return Json(response,JsonRequestBehavior.AllowGet);
    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-09
      • 2014-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-03
      • 2014-03-25
      • 2011-12-19
      相关资源
      最近更新 更多