源码下载

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script src="jquery.js" type="text/javascript"></script>
  <style type="text/css">
        .hover
        {
            cursor: pointer; /*小手*/
            background: #ffc; /*背景*/
        }
        .button
        {
            width: 150px;
            float: left;
            text-align: center;
            margin: 10px;
            padding: 10px;
            border: 1px solid #888;
        }
        #dictionary
        {
            text-align: center;
            font-size: 18px;
            clear: both;
            border-top: 3px solid #888;
        }
        #loading
        {
            border: 1px #000 solid;
            background-color: #eee;
            padding: 20px;
            margin: 100px 0 0 200px;
            position: absolute;
            display: none;
        }
        #switcher
        {
        }
    </style>
<script type="text/javascript">

//处理返回XML格式的数据
 $(document).ready(function () {
        $('#btn6').click(function () { //为DIV绑定激发事件
            $.ajax({
                type: "POST",   //访问WebService使用Post方式请求
                url: "wsPOI.asmx/GetPOIList", //调用WebService的地址和方法名称组合 ---- WsURL/方法名
                data: "{}",         //这里是要传递的参数,格式为 data: "{paraName:paraValue}",下面将会看到      
                dataType: 'xml',
                success: function (result) {     //回调函数,result,返回值

                    xmlDOC = new ActiveXObject("Microsoft.XMLDOM"); //创建XML文档对象
                    //关闭异步加载,这样确保在文档完全加载之前解析器不会继续脚本的执行
                    xmlDOC.async = false;
                    //告知解析器加载名为 "test.xml" 的 XML 文档
                    xmlDOC.loadXML(result.text);

                    var root = xmlDOC.documentElement; //获取XML文档根节点POIS
                    var lxrs = root.childNodes; //获取根节点(POIS)下面所有子节点(POI)的集合
                   
                    for (var i = 0; i < lxrs.length; i++) { //循环读取POI节点
                        var lxr = lxrs[i].childNodes;
                        $('#dictionary').append("新POI<br/>");

                        for (var j = 0; j < lxr.length; j++) {//循环读取POI下面的节点数据
                            $('#dictionary').append(lxr[j].nodeName + " : " + lxr[j].text+"<br/>"); //输出节点内容
                        }
                    }
                }
            });
        });
    });

    //无参数调用
    $(document).ready(function () {
        $('#btn1').click(function () {
            $.ajax({
                type: "POST",   //访问WebService使用Post方式请求
                contentType: "application/json", //WebService 会返回Json类型
                url: "wsPOI.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: "wsPOI.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: "wsPOI.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: "wsPOI.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: "wsPOI.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();
        });
    });

    // 鼠标移入移出效果,多个元素的时候,可以使用“,”隔开
    $(document).ready(function () {
        $('div.button').hover(function () {
            $(this).addClass('hover');
        }, function () {
            $(this).removeClass('hover');
        });
    });


</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
               
    </div>


     <div id="switcher">
        <h2>
            jQuery 的WebServices 调用</h2>
        <div class="button" id="btn1">
            HelloWorld</div>
        <div class="button" id="btn2">
            传入参数</div>
        <div class="button" id="btn3">
            返回集合</div>
        <div class="button" id="btn4">
            返回复合类型</div>
        <div class="button" id="btn5">
            返回DataSet(XML)</div>
         <div  class="button" id="btn6">
            以字符串的形式返回XML数据(POI功能测试)</div>
    </div>
    <div id="loading">
        服务器处理中,请稍后。
    </div>
    <div id="dictionary"></div>
    </form>
</body>
</html>

posted on 2011-10-22 13:04  大鱼  阅读(261)  评论(0编辑  收藏  举报