后台代码:
1[WebMethod]2publicstaticstringCreateDate(stringname,stringage)3{4return"姓名:"+name +"年龄:"+age;5}67[WebMethod]8publicstaticstringCreateInfo()9{10return"调用成功";11}1213[WebMethod]14publicstaticList<string> CreateString(stringnum)15{16List<string> list =newList<string>();17for(inti =0; i < Convert.ToInt32(num); i++)18{19list.Add(i+",");20}21returnlist;22}
前台代码:
1</div>2<input type="button" id="btn" value="点击调用后台无参方法" />3<input type="button" id="btn1" value="点击调用后台有参方法" />4<input type="button" id="btn2" value="点击调用后台返回集合的方法" />5</form>67<script>89$(function() {10$("#btn").click(function() {11$.Ajax({12type:"Post",13url: "index1.aspx/CreateInfo",14contentType: "application/json; charset=utf-8",15dataType: "json",16data: "",17success:function(data) {18alert(data.d);19},20error:function(err){21alert("调用失败")22}23});24});2526$(function() {27$("#btn1").click(function() {28$.ajax({29type: "Post",30url: "index1.aspx/CreateDate",31contentType: "application/json; charset=utf-8",32dataType: "json",33data: "{'name':'符越涵','age':'25'}",34success:function(good) { alert(good.d) },35error:function(err) { alert("调用错误") }36});37});38});3940$("#btn2").click(function() {41$.ajax({42type: "Post",43url: "index1.aspx/CreateString",44contentType: "application/json; charset=utf-8",45dataType: "json",46data:"{'num':'50'}",47success:function(data) {48$(data.d).each(function() {49alert(this)50});51},52error:function(err) {alert("调用错误") }53});54});55});56</script>