• Jquery + Ajax 调用 webService


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


            
    //有参数调用
            $(document).ready(function() {
                $(
    "#btn2").click(function() {
                    $.ajax({
                        type: 
    "POST",
                        contentType: 
    "application/json",
                        url: WebServiceURL 
    + "WebService1.asmx/GetWish",
                        data: 
    "{value1:'心想事成',value2:'万事如意',value3:'牛牛牛',value4:2009}",
                        dataType: 
    'json',
                        success: 
    function(result) {
                            $(
    '#dictionary').append(result.d);
                        }
                    });
                });
            });


            
    //返回集合(引用自网络,很说明问题)
            $(document).ready(function() {
                $(
    "#btn3").click(function() {
                    $.ajax({
                        type: 
    "POST",
                        contentType: 
    "application/json",
                        url: WebServiceURL 
    + "WebService1.asmx/GetArray",
                        data: 
    "{i:10}",
                        dataType: 
    'json',
                        success: 
    function(result) {
                            $(result.d).each(
    function() {
                                
    //alert(this);
                                $('#dictionary').append(this.toString() + " ");
                                
    //alert(result.d.join(" | "));
                            });
                        }
                    });
                });
            });


            
    //返回复合类型
            $(document).ready(function() {
                $(
    '#btn4').click(function() {
                    $.ajax({
                        type: 
    "POST",
                        contentType: 
    "application/json",
                        url: WebServiceURL 
    + "WebService1.asmx/GetClass",
                        data: 
    "{}",
                        dataType: 
    'json',
                        success: 
    function(result) {
                            $(result.d).each(
    function() {
                                
    //alert(this);
                                $('#dictionary').append(this['ID'+ " " + this['Value']);
                                
    //alert(result.d.join(" | "));
                            });

                        }
                    });
                });
            });

            
    //返回DataSet(XML)
            $(document).ready(function() {
                $(
    '#btn5').click(function() {
                    $.ajax({
                        type: 
    "POST",
                        url: WebServiceURL 
    + "WebService1.asmx/GetDataSet",
                        data: 
    "{}",
                        dataType: 
    'xml'//返回的类型为XML ,和前面的Json,不一样了
                        success: function(result) {
                            
    //演示一下捕获
                            try {
                                $(result).find(
    "Table1").each(function() {
                                    $(
    '#dictionary').append($(this).find("ID").text() + " " + $(this).find("Value").text());
                                });
                            }
                            
    catch (e) {
                                alert(e);
                                
    return;
                            }
                        },
                        error: 
    function(result, status) { //如果没有上面的捕获出错会执行这里的回调函数
                            if (status == 'error') {
                                alert(status);
                            }
                        }
                    });
                });
            });




            
    //Ajax 为用户提供反馈,利用ajaxStart和ajaxStop 方法,演示ajax跟踪相关事件的回调,他们两个方法可以添加给jQuery对象在Ajax前后回调
            //但对与Ajax的监控,本身是全局性的
            $(document).ready(function() {
                $(
    '#loading').ajaxStart(function() {
                    $(
    this).show();
                }).ajaxStop(
    function() {
                    $(
    this).hide();
                });
            });

  • 相关阅读:
    java陷阱一箩筐面试
    应用MVC设计模式解决J2ME应用程序导航问题 mingjava
    我怎么了
    运营商掘宝手机游戏 无限“钱途”引人竞折腰
    网络工程师考试大纲
    java 正则
    java集合使用——LinkedList
    java集合使用——HashMap
    java集合使用——HashSet
    JAVA集合使用——基本概念
  • 原文地址:https://www.cnblogs.com/yangsoon/p/3199939.html
Copyright © 2020-2023  润新知