0
[{"id":1,"name":"ABC"},{"id":2,"name":"XYZ"}]

这是控制器以 json 格式返回数据的方式

我想以以下格式存储它:

var json = {
"users": [
             { "id": "1", "name": "ABC" },
             { "id": "2", "name": "XYZ" },
         ]
       }

以下是我使用但不起作用的代码。

  var json = 
  {
     "users": $.getJSON("@Url.Action("SearchCMSAdmins")")
  }              

 $("#DistributorCMSAdmin").tokenInput("@Url.Action("SearchWithName")", {
    theme: "facebook",                
    preventDuplicates: true,
    prePopulate:json.users
 });

任何帮助表示赞赏。提前致谢。

4

1 回答 1

0

$.getJson 是异步的,因此您需要使用成功回调来获取响应并执行操作。试试这个。

$.getJSON("@Url.Action("SearchCMSAdmins")",function(data){
    var json= {"users":data};
    $("#DistributorCMSAdmin").tokenInput("@Url.Action("SearchWithName")", {
       theme: "facebook",                
       preventDuplicates: true,
       prePopulate:json.users
    });
});
于 2013-05-16T17:14:18.293 回答