代码之家  ›  专栏  ›  技术社区  ›  Matt

Json没有返回可作为数组访问的结果

  •  0
  • Matt  · 技术社区  · 16 年前

        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult GetCurrent()
        {
            IList<string> profile = new List<string>();
            profile.Add("-1");
            profile.Add("Test");
            profile.Add("");
    
            return this.Json(profile);
        }
    

    这篇jquery ajax文章将调用它:

    $.post("/Profile/GetCurrent", function(profile) { profileCompleteOpen(profile); }, "json");
    

    以及在post回调中调用的javascript函数:

    function profileCompleteOpen(profile) {
       alert(profile);
       alert(profile[0]);
    }
    

    第一个警报的结果显示如下阵列:

    [“-1”、“测试”、“测试”]

    [

    而不是

    -1

    谢谢
    马特

    3 回复  |  直到 16 年前
        1
  •  1
  •   Terje    16 年前

    尝试使用eval()将概要文件中的json数据转换为适当的对象。

    例子:

    var profileObject = eval('(' + profile + ')');
    
        2
  •  0
  •   griegs    16 年前

    嗯,我会做你想做的事情,稍微有点不同。

    class MyObj
    {
      public string name{get;set;}
    }
    

    填充对象并将其作为json对象返回。然后您就知道jquery代码可以像任何其他对象一样访问。

    另一种方法可能是做一个测试 return PartialView("MyView", model);

        3
  •  0
  •   m3kh    16 年前

    我认为 profile 是字符串而不是数组。为什么?检查 $.post 方法参数。也许问题就在那里。

    $.post("url", null, function(profile) { ... }, "json");