Ajax--WebService返回List

WebService:

    using System.Web.Script.Services;
    [GenerateScriptType(typeof(people))]

    [WebMethod]
    public List<people> GetPeopleList()
    {
        List<people> list = new List<people>();
        for (int i = 0; i < 10; i++)
        {
            people p = new people();
            p.Name = "我是" + i.ToString();
            list.Add(p);
        }

        return list;
    }

 

ScriptManager:

    <asp:ScriptManager ID="sm" runat="server">
    <Services>
        <asp:ServiceReference Path="~/WebService2.asmx" InlineScript="true" />
    </Services>
    </asp:ScriptManager>

 

页面控件:

    <input type="button" value="添加" onclick="btn_click()" id="btn" />
    <div id="result">
    
    </div>

 

Js:

    <script type="text/javascript">
        
        function btn_click(){
            WebService2.GetPeopleList(onSuccessed) 
        }
        
        function onSuccessed(result){
            for(var i=0;i<result.length;i++){
                var label = document.createElement("label");
                label.innerHTML=result[i]["Name"]+"<br/>";
                $get("result").appendChild(label);
            }
        }
                
    </script>

 

结果:

image

 

debugger:

image

posted @ 2015-05-17 14:06  海绵宝  阅读(504)  评论(0编辑  收藏  举报