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>
結果:
debugger: