
·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> Ajax调用asp.net后台代码
后台代码:
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>