为什么我无法从C#asp.net传递的javascript中获取list中的值

时间:2015-08-29 12:44:35

标签: javascript c# asp.net arraylist

这是C#中的代码,GetCityNameArray返回一个列表

   [System.Web.Services.WebMethod]
    public static  List<NegaraLibrary.Model.User.AdminUser> GetCityNameArray(       List<NegaraLibrary.Model.User.AdminUser> cities)
    {
        load();
        return adminuserlist;
    }

这是Javascript代码:

     <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
   <script>
    function GetCityNameArray() {
        var cities = new Array();
        var city = {};
        city.AdminUserName = "2000";
        city.AdminUserEmail = "safda";
        city.AdminUserID = 55;
        city.AdminUserPassword = "5555s";
        city.AdminUserTypeID = 1;
        city.CreatedOn = "12/7/2015";
        city.ModifiedOn = "12/7/2015";
        city.IsActive = true;
        cities[0] = city;
        city = {};
        city.AdminUserName = "2000";
        city.AdminUserEmail = "safda";
        city.AdminUserID = 55;
        city.AdminUserPassword = "5555s";
        city.AdminUserTypeID = 1;
        city.CreatedOn = "12/7/2015";
        city.ModifiedOn = "12/7/2015";
        city.IsActive = true;
        cities[1] = city;
        PageMethods.GetCityNameArray(cities, OnSuccessGetCityNameArray);
    }
    function OnSuccessGetCityNameArray(response)
    {
        alert(response.length);
        for (var i in response)
        {
            alert(response[i].AdminUserPassword + "," + response[i].AdminUserEmail + "," + response[i].AdminUserName + "," +   response[i].AdminUserID + "," + response[i].AdminUserTypeID);
        }
    }
  </script>

当从C#Javascript传递arraylist时,它在警告框中显示为undefined。 当我试图获得传递数组的长度时,我得到了令人难以置信的数字。为什么会这样?

1 个答案:

答案 0 :(得分:0)

试试这个

function OnSuccessGetCityNameArray(response)
    {
response = JSON.parse(response);
        alert(response.length);
        for (var i in response)
        {
            alert(response[i].AdminUserPassword + "," + response[i].AdminUserEmail + "," + response[i].AdminUserName + "," +   response[i].AdminUserID + "," + response[i].AdminUserTypeID);
        }
    }