jquery中通过ajax调用webservice传递数组参数的问题

来源:互联网 发布:php cookie能存多少 编辑:程序博客网 时间:2024/05/22 12:38

如题.

还是直接用例子说明来的直接些.

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

客户端代码如下:

 1             $.ajax({ 2                 url: "test/xxx.asmx", 3                 type: 'POST', 4                 dataType: 'xml', 5                 timeout: 1000, 6                 data: { name: "zhangsan", tags: ["aa", "bb", "cc"] }, 7                 error: function(xml) { 8                     alert(xml.responseText); 9                 },10                 success: function(xml) {11                     alert("OK");12                 }13 14             });

服务端代码如下:

1   [WebMethod]2     public XmlDocument xxx(string name, string [] tags )3     { 4          return sth;  5     }

总是抛出异常.

问题出现在这里:

下面是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)

 1     function buildParams(prefix, obj, traditional, add) { 2         var name; 3  4         if (jQuery.isArray(obj)) {  5             // Serialize array item. 6             jQuery.each(obj, function(i, v) { 7                 if (traditional || rbracket.test(prefix)) {                     8                     // Treat each array item as a scalar. 9                     add(prefix, v);10 11                 } else {12                     // If array item is non-scalar (array or object), encode its13                     // numeric index to resolve deserialization ambiguity issues.14                     // Note that rack (as of 1.0.0) can't currently deserialize15                     // nested arrays properly, and attempting to do so may cause16                     // a server error. Possible fixes are to modify rack's17                     // deserialization algorithm or to provide an option or flag18                     // to force array serialization to be shallow.19                     20                     //ytx 2013041121                     buildParams(prefix, v, traditional, add);22                     //buildParams(prefix + "[" + (typeof v === "object" ? i : "") + "]", v, traditional, add);23                 }24             });25 26         } else if (!traditional && jQuery.type(obj) === "object") {27             // Serialize object item.28             for (name in obj) {29                 buildParams(prefix + "[" + name + "]", obj[name], traditional, add);30             }31 32         } else {33             // Serialize scalar item.34             add(prefix, obj);35         }36     }

结论:

出问题的代码在22行,我修改成21行那样就行了.

不过,我对js/jquery都是一知半解的,希望不要引起别的后遗症,呵呵.

 

 

 

 


<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>