如何从ajax调用中检索值数组

时间:2013-11-26 12:39:30

标签: javascript jquery ajax

我想在ajax成功方法中检索值数组....

这是我的ajax代码

 $.ajax({
    url: '/Workflows/NumberofWorkflows',
    type: 'GET',
    async: false,
    dataType: 'text',
    processData: false,
    //contentType: 'application/json; charset=utf-8',
    //data: "input=" + JSON.stringify(IVRInstant.data) + "&workflowname=" + workflowname,
    success: function (data) {
        alert(data[0]);
    }
});

这是我的控制器

public string[] NumberofWorkflows()
 {
        string[] names = { };
        var email = Session["Username"];

        var userid = (from a in db.users
                      where a.userName == email
                      select a.userId).SingleOrDefault();


        names = (from s in db.workflows
                 where s.userId == userid
                 select s.Name).ToArray();
        return names;
    }

names变量包含值列表,但我没有在ajax方法中接收它

2 个答案:

答案 0 :(得分:0)

你有没有尝试过:

var arr = $.parseJSON(data);

success: function (data) {
    var arr = $.parseJSON(data);
    alert(arr[0]);
}

答案 1 :(得分:0)

id_numbers = new Array();
 $.ajax({
   url:"Example.php",
   type:"POST",
  success:function(msg){
    id_numbers = msg;
  },
  "json"
});

<强>的JavaScript

id_numbers = JSON.parse(msg);

<强> PHP

echo json_encode($id_numbers);

您可以为jQuery提供第四个参数,以便为您自动解码JSON。