失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > jQuery中通过ajax调用webservice传递数组参数的问题(图文教程)

jQuery中通过ajax调用webservice传递数组参数的问题(图文教程)

时间:2022-01-27 20:27:52

相关推荐

jQuery中通过ajax调用webservice传递数组参数的问题(图文教程)

web前端|js教程

webservice,jQuery,ajax

web前端-js教程

本文通过实例给大家详细介绍jQuery中通过ajax调用webservice传递数组参数的相关资料,需要的朋友可以参考下

.net整站源码下载,ubuntu 断电后 时间,tomcat自定义会话管理器,云南爬虫网络订餐,抽奖大转盘怎么做PHP,盘龙区seo怎样lzw

下面通过实例给大家说明比较直观些,更方便大家了解。

易语言*8 6计算器源码,vscode默认插件位置,ubuntu卡在开机,卸载本机的tomcat,python 并发爬虫,php时间转秒,seo优化服务费用明细,照明灯企业网站织梦模板,动易sf模板下载lzw

本人的项目中通过jquery.ajax调用webservice.

vpn计费管理系统源码,vscode日语翻译插件,ubuntu 查看mesa,tomcat改发布名,爬虫gbk乱码,php远程工作,网页seo排名热线电话,企业手机网站毕业源码,模板源码分享lzw

客户端代码如下:

$.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 /xxx.asmx/xxx HTTP/1.1Host: center.Connection: keep-aliveContent-Length: 55Cache-Control: max-age=0Origin: User-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: /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: Content-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 currently deserialize// nested arrays properly, and attempting to do so may cause// a server error. Possible fixes are to modify racks// 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);}}

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

利用ajax传递数组及后台接收的方法详解

AJAX分页效果简单实现(图文教学)

Ajax传递特殊字符的数据如何解决

如果觉得《jQuery中通过ajax调用webservice传递数组参数的问题(图文教程)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。