后臺代碼:
1 [WebMethod] 2 public static string CreateDate(string name,string age) 3 { 4 return "姓名:"+name +"年齡:"+ age; 5 } 6 7 [WebMethod] 8 public static string CreateInfo() 9 {10 return "調用成功";11 }12 13 [WebMethod]14 public static List<string> CreateString(string num)15 {16 List<string> list = new List<string>();17 for (int i = 0; i < Convert.ToInt32(num); i++)18 {19 list.Add(i+",");20 }21 return list;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> 6 7 <script> 8 9 $(function () {10 $("#btn").click(function () {11 $.Ajax({12 type:"Post",13 url: "index1.aspx/CreateInfo",14 contentType: "application/json; charset=utf-8",15 dataType: "json",16 data: "",17 success: function (data) {18 alert(data.d);19 },20 error:function(err){21 alert("調用失敗")22 }23 });24 });25 26 $(function () {27 $("#btn1").click(function () {28 $.ajax({29 type: "Post",30 url: "index1.aspx/CreateDate",31 contentType: "application/json; charset=utf-8",32 dataType: "json",33 data: "{'name':'符越涵','age':'25'}",34 success: function (good) { alert(good.d) },35 error: function (err) { alert("調用錯誤") }36 });37 });38 });39 40 $("#btn2").click(function () {41 $.ajax({42 type: "Post",43 url: "index1.aspx/CreateString",44 contentType: "application/json; charset=utf-8",45 dataType: "json",46 data:"{'num':'50'}",47 success: function (data) {48 $(data.d).each(function () {49 alert(this)50 });51 },52 error: function (err) {alert("調用錯誤") }53 });54 });55 });56 </script>
新聞熱點
疑難解答