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>
结果:
debugger:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。