Jquery + Ajax 調用 webService

來源:互聯網
上載者:User

webService中要實現ajax調用,則要加這句代碼:

    // 若要允許使用 ASP.NET AJAX 從指令碼中調用此 Web 服務,請取消對下行的注釋。
    [System.Web.Script.Services.ScriptService] 

 代碼下載

 

代碼


        //無參數調用
        $(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();
            });
        });

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.