jQuery入門(9):Ajax(調用頁面方法和WebService)

來源:互聯網
上載者:User

調用頁面方法和webservice

註:

web.config中要指定允許調用web服務。

必須為Post方式來調用。

WEB服務返回的為XML串,所以用戶端要轉換為JSON再處理。

        [WebMethod]

        public static string AjaxServiceTest(string str)

        {

            return string.Format("Hello,{0}", str);

        } 

    [System.Web.Script.Services.ScriptService]

    public class TestWebService : System.Web.Services.WebService

    {

        [WebMethod]

        public string HelloWorld(string s)

        {

            return "Hello World" + s ;

        }

    } 

        function WebServiceMethod() {

            $.ajax({

                type: "POST",

                contentType: "application/json;utf-8",

                url: "TestWebService.asmx/HelloWorld",

                data: "{s:""" + $("#inputName").val() + """}", //傳webservice方法的參數,s是方法的參數,必須一致                                         

                success: function(data) {

                    //將返回字串轉換為json對象

                    var json = null;

                    try {

                        json = eval('(' + data + ')');

                    } catch (e) {

                        alert("返回字串不是json格式!");

                        return;

                    }

                    alert(json.d);

                }

            });

        }

        function PageAjaxServiceTest() {

            $.ajax({

                type: "POST",

                contentType: "application/json;utf-8",

                url: "PageService.aspx/AjaxServiceTest",

                data: "{str:""" + $("#inputName").val() + """}", //傳到.cs靜態方法的參數,str是方法的參數,必須一致                                    

                success: function(data) {

                    //將返回字串轉換為json對象

                    var json = null;

                    try {

                        json = eval('(' + data + ')');

                    } catch (e) {

                        alert("返回字串不是json格式!");

                        return;

                    }

                    alert(json.d);

                }

            });

        }

    </script>

    <div>

    <input id="inputName" type="text" value="" />

    <a href="#" onclick="WebServiceMethod();">調用web服務</a><br /><br />

    <a href="#" onclick="PageAjaxServiceTest();">調用頁面靜態方法</a><br /><br />

    </div> 

註:

        $.post("PagingService.asmx/GetLoginHtml", { s: "fda" },

         function(data) {

             $("#ajaxlogin").html(data);

             alert(data);

         }, "html");

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.