jQuery中通過(guò)ajax調(diào)用webservice傳遞數(shù)組參數(shù)的問(wèn)題實(shí)例詳解
來(lái)源:易賢網(wǎng) 閱讀:1334 次 日期:2016-06-25 14:56:33
溫馨提示:易賢網(wǎng)小編為您整理了“jQuery中通過(guò)ajax調(diào)用webservice傳遞數(shù)組參數(shù)的問(wèn)題實(shí)例詳解”,方便廣大網(wǎng)友查閱!

本文通過(guò)實(shí)例給大家詳細(xì)介紹jQuery中通過(guò)ajax調(diào)用webservice傳遞數(shù)組參數(shù)的相關(guān)資料,需要的朋友可以參考下

下面通過(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上面了.見(jiàn)代碼(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ì)大家有所幫助

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢(xún)回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢(xún)?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽(tīng)報(bào)名

  • 報(bào)班類(lèi)型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢(xún) | 簡(jiǎn)要咨詢(xún)須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專(zhuān)用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢(xún)關(guān)注公眾號(hào):hfpxwx
咨詢(xún)QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專(zhuān)用圖標(biāo)