Ajax调用asp.net后台代码_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > Ajax调用asp.net后台代码

Ajax调用asp.net后台代码

 2014/4/23 21:44:45  何其菜  博客园  我要评论(0)
  • 摘要:后台代码:1[WebMethod]2publicstaticstringCreateDate(stringname,stringage)3{4return"姓名:"+name+"年龄:"+age;5}67[WebMethod]8publicstaticstringCreateInfo()9{10return"调用成功";11}1213[WebMethod]14publicstaticList<string>CreateString(stringnum)15{16List<
  • 标签:.net ASP.NET net Ajax 代码

后台代码:

 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>

 

 

发表评论
用户名: 匿名