jQuery中通過(guò)ajax調(diào)用webservice傳遞數(shù)組參數(shù)的問(wèn)題實(shí)例詳解
下面通過(guò)實(shí)例給大家說(shuō)明比較直觀些,更方便大家了解。
本人的項(xiàng)目中通過(guò)jquery.ajax調(diào)用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"); } });
服務(wù)端代碼如下:
[WebMethod] public XmlDocument xxx(string name, string [] tags ) { return sth; }
總是拋出異常.
問(wèn)題出現(xiàn)在這里:
下面是HTTP數(shù)據(jù):
POST http://xxx.com/xxx.asmx/xxx HTTP/1.1 Host: center.cmis.htpc.com.cn Connection: keep-alive Content-Length: 55 Cache-Control: max-age=0 Origin: http://xxx.com User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.186 Safari/535.1 Content-Type: application/x-www-form-urlencoded; charset=UTF-8 Accept: application/xml, text/xml, */*; q=0.01 Referer: http://xxx.com/xxx.aspx Accept-Encoding: gzip,deflate,sdch Accept-Language: zh-CN,zh;q=0.8 Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3 name=zhangsan&tags%5B%5D=aa&tags%5B%5D=bb&tags%5B%5D=cc
而它期望的格式是如下的:
POST /xxx.asmx/xxx HTTP/1.1 Host: xxx.com Content-Type: application/x-www-form-urlencoded Content-Length: length name=string&tags=string&tags=string
比較上面粗體,post的數(shù)據(jù)除了問(wèn)題. 正確的應(yīng)該如下:
name=zhangsan&tags=aa&tags=bb&tags=cc
看來(lái)問(wèn)題出在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); } }
結(jié)論:
出問(wèn)題的代碼在22行,我修改成21行那樣就行了.
不過(guò),我對(duì)js/jquery都是一知半解的,希望不要引起別的后遺癥,呵呵.
以上所述是小編給大家介紹的jQuery中通過(guò)ajax調(diào)用webservice傳遞數(shù)組參數(shù)的問(wèn)題實(shí)例詳解的相關(guān)知識(shí),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- js傳遞數(shù)組參數(shù)到后臺(tái)controller的方法
- php實(shí)現(xiàn)向javascript傳遞數(shù)組的方法
- JavaScript實(shí)現(xiàn)shuffle數(shù)組洗牌操作示例
- C語(yǔ)言數(shù)組棧實(shí)現(xiàn)模板
- C語(yǔ)言結(jié)構(gòu)體數(shù)組同時(shí)賦值的另類用法
- C語(yǔ)言數(shù)組和指針的問(wèn)題一道非常值得深思的筆試題
- C語(yǔ)言數(shù)組按協(xié)議存儲(chǔ)與按協(xié)議解析數(shù)據(jù)的實(shí)現(xiàn)
- C++實(shí)現(xiàn)動(dòng)態(tài)數(shù)組功能
- C++小知識(shí):C/C++中不要按值傳遞數(shù)組
相關(guān)文章
最常見的左側(cè)分類菜單欄jQuery實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了最常見的左側(cè)分類菜單欄jQuery實(shí)現(xiàn)代碼,仿京東、淘寶等各大類網(wǎng)站效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11JQuery勾選指定name的復(fù)選框集合并顯示的方法
這篇文章主要介紹了JQuery勾選指定name的復(fù)選框集合并顯示的方法,涉及jQuery表單元素操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-05-05jQuery學(xué)習(xí)2 選擇器的使用說(shuō)明
利用選擇器,選擇要被操作的html中的元素。2010-02-02jQuery對(duì)象和DOM對(duì)象相互轉(zhuǎn)化
jQuery對(duì)象就是通過(guò)jQuery包裝DOM對(duì)象后產(chǎn)生的對(duì)象。jQuery對(duì)象是jQuery獨(dú)有的,其可以使用jQuery里的方法,但是不能使用DOM的方法;例如$("#img").attr("src","test.jpg"); 這里的$("#img")就是jQuery對(duì)象;2009-04-04jQuery、layer實(shí)現(xiàn)彈出層的打開、關(guān)閉功能
這篇文章主要介紹了jQuery、layer實(shí)現(xiàn)彈出層的打開、關(guān)閉功能,需要的朋友可以參考下2017-06-06jquery實(shí)現(xiàn)網(wǎng)頁(yè)左側(cè)導(dǎo)航菜單欄
這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)網(wǎng)頁(yè)左側(cè)導(dǎo)航菜單欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07jquery將一個(gè)表單序列化為一個(gè)對(duì)象的方法
將一個(gè)表單序列化為一個(gè)對(duì)象的方法有很多,感興趣的朋友可以了解下本文所介紹的這個(gè),希望對(duì)大家有所幫助2013-12-12