zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    this_is_flag
    攻防世界-misc-如来十三掌
    攻防世界-misc-pdf
    nextcloud取消新用户的默认文件
    nextcloud开放注册-添加注册功能
    图片马制作
    Npoi Web 项目中(XSSFWorkbook) 导出出现无法访问已关闭的流的解决方法
    VS2017 如何安装水晶报表 VS2017 如何下载相应版本的水晶报表 VS2017 水晶报表 中文乱码
    js 带有返回值的 匿名方法
    varchar nvarchar 设计长度时 设计成 (2^n)-1 的好处
  • 原文地址:https://www.cnblogs.com/fuyuehangogo/p/3683455.html
Copyright © 2011-2022 走看看