• 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>
  • 相关阅读:
    MFC菜单快捷键的应用
    TDD in C++
    Mapping
    初入股市者怎样看盘
    C++随笔分类列表(高级)
    C++代码优化
    IT生活
    十一年炒股的感悟
    框架设计(第2版)CLR Via C#(1)
    Visual Assist X自己常用的快捷功能
  • 原文地址:https://www.cnblogs.com/fuyuehangogo/p/3683455.html
Copyright © 2020-2023  润新知