【问题标题】:What should my JSON string look like in my Action?我的 JSON 字符串在我的 Action 中应该是什么样的?
【发布时间】:2009-02-19 04:48:21
【问题描述】:

在 .NET MVC 中,我的操作如下所示:

public ActionResult TestAjax(string testID)
{

    return Content(@"{first: ""1"", second : ""2""}");
}

在我的 JavaScript 中我正在做:

function(data)
{
      alert(data.first);
}

我得到[object Object] 作为输出,这是为什么呢?

我的 JSON 字符串错了吗?

【问题讨论】:

    标签: asp.net-mvc json actionresult


    【解决方案1】:

    让系统处理怎么样:

        public ActionResult TestAjax(string testID)
        {
            return Json(new {first = 1, second = 2});
        }
    

    【讨论】:

      【解决方案2】:

      你想用 Json 而不是 Content 进行返回

      return Json(new { first = "1", second ="2" });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-09
        • 2017-12-14
        • 1970-01-01
        • 2020-02-23
        • 2019-12-07
        • 2013-01-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多