Jquery 調用.net WebService 返回Json、XML方法

來源:互聯網
上載者:User

標籤:

由於項目需要,使用前端、手機用戶端調用ASP.NET的Webservice來擷取資訊.所以這段時間重溫一下Jquery與Web Serivce,過程中碰到不少問題,也有不少的收穫。

Service代碼 

        [WebMethod]        public string HelloWorld()        {            return "Hello World";        }         [WebMethod]        public string myMethod(string para)        {            return "Hi," + para;        }        [WebMethod]        [ScriptMethod(UseHttpGet = true)]        public string myMethod1(string para)        {            return "Hi," + para;        }

  

Web端調用方法

1.Json POST方法(有參)

$.ajax({            url: "WebServer.asmx/myMethod",            contentType: "application/json;charset=utf-8",            beforeSend: function (x) {                x.setRequestHeader("Content-Type", "application/json;charset=utf-8");            },            data: "{para:‘post參數‘}",            dataType: "json",            type: "POST",            error: function (x, e) {                alert(x.responseText);            },            success: function (json) {                alert(json.d);            }        });

注意:1) ajax中的data:"{paraName:paraValue}",如果該方法無參數,則格式為:data:"{}",再次注意還有紅色的兩引號,不可少

         2) 如果成功,我是以HTML的形式顯示它的值,大家可以用其它方法,取它的值時用(result.d)

2.Json GET方法(有參)

        $.ajax({            url: "webserver.asmx/myMethod1",            data: "para=‘get參數‘",            type: "GET",            contentType: "application/json;charset=utf-8",            dataType: "json",            error: function (x, e) {                alert(x.responseText);            },            success: function (json) {                alert(json.d);            }        });

注意:1) ajax中的data:"para=paraValue",如果有漢字的參數值,可以使用encodeURI編一下碼,如果該方法無參數,則格式為:data:""

         2) 如果成功,取它的值時用(result.d)

3.XML POST方法(有參)

$.ajax({            url: "webserver.asmx/myMethod",            data: {para: ‘post參數‘},            type: "POST",            dataType: "application/xml",            error: function (x, e) {                alert(x.responseText);            },            success: function (xml) {                alert(xml);            }        });

 這塊,data中的para的值為實值型別,去掉單引號也可以,不會報錯,小弟特意測試過,如果你們去掉後出錯了,請給小弟留言說明,當para的值為string類型時,單引號不可去掉,如果是中文最好編一下碼,操作成功後返回XML文檔,其他地方就沒什麼可說的了。

4.XML GET 方法 (有參)

$.ajax({            url: "webserver.asmx/myMethod1",            data: encodeURI("para=Post參數"),            type: "GET",            dataType: "application/xml",            error: function (x, e) {                alert(x.responseText);            },            success: function (xml) {                alert(xml);            }        });

這裡需要注意,ajax中的data:"para=paraValue",如果有漢字的參數值,最好encodeURI編一下碼,操作成功後返回XML文檔。

5.XML SOAP POST方法

function getPostData() {        //根據WSDL分析myMethod是方法名,para是傳入參數名         var postdata = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";        postdata += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";        postdata += "<soap:Body>";        postdata += "<myMethod xmlns=\"http://tempuri.org/\">";        postdata += "<para>"+"aa"+"</para>";        postdata += "</myMethod>";        postdata += "</soap:Body>";        postdata += "</soap:Envelope>";        return postdata;    } 
$.ajax({                    type: "POST",                    url: "webserver.asmx",                    data: getPostData(),                     dataType: "xml",                     contentType: "text/xml; charset=utf-8",                    beforeSend: function (xhr) {                        xhr.setRequestHeader(‘SOAPAction‘, ‘http://tempuri.org/myMethod‘);                    },                    success: function (xml) {                        $(xml).find("myMethod_XMLResult").each(function (i) {                            alert($(this).children("Title").text());                        });                    },                    error: function (x, e) {                        alert(‘error:‘ + x.responseText);                    },                    complete: function (x) {                        //alert(‘complete:‘+x.responseText);                     }                });

使用SOAP協議時,請注意上面POST的地址後面並沒有像請求JSON資料時一樣加上/方法名,而SOAPAction的方法名前面還需要加上命名空間,操作成功後返回的是一個XML格式對象而不是XML文檔,可以使用JS來解析XML對象

總結:

1.使用POST方法時,要注意data的參數格式

2.參數中有漢字時最好encodeURI編碼

3.使用SOAP時,注意POST地址後面沒有/方法名,並且需要在SOAPAction的方法名前面還需要加上命名空間,否則會報錯

以上是小弟參考各位大神資料及自己在測試中的問題解決的總結,如有錯請指正。

Jquery 調用.net WebService 返回Json、XML方法

聯繫我們

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