The problem of passing array parameters through Ajax call WebService in jquery

Source: Internet
Author: User
Tags scalar

Title.

or directly using examples to illustrate the direct.

I called WebService through Jquery.ajax in my project.

The client code is as follows:
1 $.ajax ({2URL: "Test/xxx.asmx",3Type: ' POST ',4DataType: ' xml ',5timeout:1000,6Data: {name: "Zhangsan", Tags: ["AA", "BB", "CC"] },7Errorfunction(XML) {8 alert (xml.responsetext);9                 },TenSuccessfunction(XML) { OneAlert ("OK"); A                 } -  -});
The service-side code is as follows:
1   [WebMethod]2      Public XmlDocument xxx (stringstring  [] tags)3    4          return  sth;   5     }

Always throws an exception.

The problem arises here:

Here 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-CNZh;q=0.8Accept-charset:gbk,utf-8;q=0.7,*;q=0.3 name=zhangsan&tags%5b%5d=aa&tags%5b%5d=bb&tags%5b%5d=cc

And the format it expects is the following:

Post/xxx.asmx/xxx http/1.1Host:xxx.comcontent-type:application/x-www-form-urlencodedcontent-length:length name  =string&tags=string&tags=string

Compare the above bold, post data in addition to the problem. The correct should be as follows:

Name=zhangsan&tags=aa&tags=bb&tags=cc

It seems that the problem is above the Jquery.ajax. See Code (JQUERY.1.8.3.JS)

1     functionbuildparams (prefix, obj, traditional, add) {2         varname;3 4         if(Jquery.isarray (obj)) {5             //Serialize array item.6Jquery.each (obj,function(i, v) {7                 if(Traditional | |rbracket.test (prefix)) {                    8                     //Treat each array item as a scalar.9 Add (prefix, v);Ten  One}Else { A                     //If Array Item is non-scalar (array or object), encode its -                     //Numeric Index To resolve deserialization ambiguity issues. -                     //Note that rack (as of 1.0.0) can ' t currently deserialize the                     //nested arrays properly, and attempting to doing so may cause -                     //a server error. Possible fixes is to modify rack ' s -                     //deserialization algorithm or to provide an option or flag -                     //To force array serialization to is shallow. +                      -                     //Ytx 20130411 + buildparams (prefix, V, traditional, add); A                     //buildparams (prefix + "[" + (typeof v = = = "Object"?) I: "") + "]", V, Traditional, add); at                 } -             }); -  -}Else if(!traditional && jquery.type (obj) = = = = "Object") { -             //Serialize Object Item. -              for(Nameinchobj) { inBuildparams (prefix + "[" + Name + "]", Obj[name], traditional, add); -             } to  +}Else { -             //Serialize scalar item. the Add (prefix, obj); *         } $}
Conclusion:

The code of the problem is in line 22, I change it to 21 lines.

However, I am smattering to js/jquery, hope not to cause other sequelae, hehe.

The problem of passing array parameters through Ajax call WebService in jquery

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.