zoukankan      html  css  js  c++  java
  • jQuery调用ASP.NET的WebService

    1、无参数调用

             $(document).ready(function() {
                $('#Button1').click(function() {
                    $.ajax({
                        type: "POST",   //访问WebService使用Post方式请求
                        contentType: "application/json", //WebService 会返回Json类型
                        url: "../WebService.asmx/HelloWorld", //调用WebService地址和方法名称组合---WsURL/方法名
                        data: "{}",         //这里是要传递的参数,格式为 data: "{paraName:paraValue}",下面将会看到      
                        dataType: 'json',
                        success: function(result) {     //回调函数,result,返回值

                               alert(result.d);
                         }
                    });

                  });

              });

          2、带参数的调用

            

             $(document).ready(function() {
                $('#Button1').click(function() {
                    $.ajax({
                        type: "POST",   //访问WebService使用Post方式请求
                        contentType: "application/json", //WebService 会返回Json类型
                        url: "../WebService.asmx/HelloWorld", //调用WebService地址和方法名称组合---WsURL/方法名
                        data: "{userName:'alpha'}", 
                        dataType: 'json',
                        success: function(result) {     //回调函数,result,返回值

                               alert(result.d);
                        }
                    });

                  });

              });

          3、返回复合类型        

             $(document).ready(function() {
                $('#Button1').click(function() {
                    $.ajax({
                        type: "POST",   //访问WebService使用Post方式请求
                        contentType: "application/json", //WebService 会返回Json类型
                        url: "../WebService.asmx/GetClass", //调用WebService地址和方法名称组合---WsURL/方法名
                        data: "{}",
                        dataType: 'json',
                        success: function(result) {     //回调函数,result,返回值

                               alert(result.d["StuName"]);
                        }
                    });

                  });

              });

          4、返回泛型集合        

             $(document).ready(function() {
                $('#Button1').click(function() {
                    $.ajax({
                        type: "POST",   //访问WebService使用Post方式请求
                        contentType: "application/json", //WebService 会返回Json类型
                        url: "../WebService.asmx/GetList", //调用WebService地址和方法名称组合---WsURL/方法名
                        data: "{}",
                        dataType: 'json',
                        success: function(result) {     //回调函数,result,返回值

                               $(result.d).each(function(){
                                    $("#result").append(this["Id"]+" "+this["StuName"]+"<br />");
                                });                   

                         }
                     });

                  });

              });

          5、返回DataSet(xml格式)

             $(document).ready(function() {
                $('#Button1').click(function() {
                    $.ajax({
                        type: "POST",   //访问WebService使用Post方式请求
                        url: "../WebService.asmx/GetDataSet", //调用WebService地址和方法名称组合---WsURL/方法名
                        data: "{}",
                        dataType: "xml",
                        success: function(result) {     //回调函数,result,返回值

                                $(result).find("Table1").each(function() {
                                    $('#result').append($(this).find("Id").text() + " " + $(this).find("Name").text()+"<br />");
                                });               

                         }
                     });

                  });

              });

  • 相关阅读:
    Thinkphp M方法出错,D方法却可以
    Composer项目安装依赖包
    wamp httpd-vhosts.conf
    博客园报错 Mixed Content: The page at 'https://i.cnblogs.com/EditPosts.aspx?opt=1' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://upload.cnblogs.com/imageuploa
    Thinkphp js、css压缩类minify
    Thinkphp 不足之处
    Thinkphp 调试方法
    Lavavel 程序报错 MassAssignmentException in Model.php line 452: _token
    Laravel 安装mysql、表增加模拟数据、生成控制器
    Laravel 安装登录模块
  • 原文地址:https://www.cnblogs.com/memaxiaofeng/p/3275482.html
Copyright © 2011-2022 走看看