In jquery, the question of passing array parameters by calling webservice via ajax is that jquerywebservice
Example.
We can directly use examples to illustrate this.
In my project, webservice is called through jquery. ajax.
The client code is as follows:
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 });
The server code is as follows:
1 [WebMethod]2 public XmlDocument xxx(string name, string [] tags )3 { 4 return sth; 5 }
Always throws an exception.
The problem occurs here:
The following is the HTTP data:
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
The expected format is as follows:
POST /xxx.asmx/xxx HTTP/1.1Host: xxx.comContent-Type: application/x-www-form-urlencodedContent-Length: lengthname=string&tags=string&tags=string
Compared with the bold text above, the post data should be correct in addition to the following:
name=zhangsan&tags=aa&tags=bb&tags=cc
It seems that the problem lies in jquery. ajax. See the code (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 }
Conclusion:
There are 22 Lines of problematic code. I can modify it to 21 lines.
However, I have no idea about js or jquery. I hope it will not cause any other sequelae.