jQuery中通過ajax調用webservice傳遞數組參數的問題執行個體詳解_jquery

來源:互聯網
上載者:User

下面通過執行個體給大家說明比較直觀些,更方便大家瞭解。

本人的項目中通過jquery.ajax調用webservice.

用戶端代碼如下:

$.ajax({url: "test/xxx.asmx",type: 'POST',dataType: 'xml',timeout: ,data: { name: "zhangsan", tags: ["aa", "bb", "cc"] },error: function(xml) {alert(xml.responseText);},success: function(xml) {alert("OK");}}); 

服務端代碼如下:

[WebMethod]public XmlDocument xxx(string name, string [] tags ){ return sth; }

總是拋出異常.

問題出現在這裡:

下面是HTTP資料:

POST http://xxx.com/xxx.asmx/xxx HTTP/1.1Host: center.cmis.htpc.com.cnConnection: keep-aliveContent-Length: 55Cache-Control: max-age=0Origin: http://xxx.comUser-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.186 Safari/535.1Content-Type: application/x-www-form-urlencoded; charset=UTF-8Accept: application/xml, text/xml, */*; q=0.01Referer: http://xxx.com/xxx.aspxAccept-Encoding: gzip,deflate,sdchAccept-Language: zh-CN,zh;q=0.8Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3name=zhangsan&tags%5B%5D=aa&tags%5B%5D=bb&tags%5B%5D=cc

而它期望的格式是如下的:

POST /xxx.asmx/xxx HTTP/1.1Host: xxx.comContent-Type: application/x-www-form-urlencodedContent-Length: lengthname=string&tags=string&tags=string

比較上面粗體,post的資料除了問題. 正確的應該如下:

name=zhangsan&tags=aa&tags=bb&tags=cc

看來問題出在jquery.ajax上面了.見代碼(jquery.1.8.3.js)

function buildParams(prefix, obj, traditional, add) {var name;if (jQuery.isArray(obj)) { // Serialize array item.jQuery.each(obj, function(i, v) {if (traditional || rbracket.test(prefix)) { // Treat each array item as a scalar.add(prefix, v);} else {// If array item is non-scalar (array or object), encode its// numeric index to resolve deserialization ambiguity issues.// Note that rack (as of ..) can't currently deserialize// nested arrays properly, and attempting to do so may cause// a server error. Possible fixes are to modify rack's// deserialization algorithm or to provide an option or flag// to force array serialization to be shallow.//ytx buildParams(prefix, v, traditional, add);//buildParams(prefix + "[" + (typeof v === "object" ? i : "") + "]", v, traditional, add);}});} else if (!traditional && jQuery.type(obj) === "object") {// Serialize object item.for (name in obj) {buildParams(prefix + "[" + name + "]", obj[name], traditional, add);}} else {// Serialize scalar item.add(prefix, obj);}}

結論:

出問題的代碼在22行,我修改成21行那樣就行了.

不過,我對js/jquery都是一知半解的,希望不要引起別的後遺症,呵呵.

以上所述是小編給大家介紹的jQuery中通過ajax調用webservice傳遞數組參數的問題執行個體詳解的相關知識,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!

相關文章

聯繫我們

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