Ajax-WebService返回简单数组
WebService:
[WebMethod] public string[] GetSimpleArray() { string[] cities = { "北京", "上海", "天津", "重庆" }; return cities; }
ScriptManager:
<asp:ScriptManager ID="sm" runat="server"> <Services> <asp:ServiceReference Path="~/WebService2.asmx" /> </Services> </asp:ScriptManager>
页面控件:
<input type="button" value="Click Me!" onclick="btn_click()" id="btn" /> <select id="st"> </select>
Js:
<script type="text/javascript"> function btn_click(){ WebService2.GetSimpleArray(onSuccessed); } function onSuccessed(result){ for(var i=document.getElementById("st").childNodes.length;i>0;i--){ $get("st").remove(i); } for(var i=0;i<result.length;i++){ var option = document.createElement("option"); option.innerHTML = result[i]; $get("st").appendChild(option); } } </script>
debugger后:
效果: