An example of a problem in jquery that calls WebService passing array parameters via Ajax _jquery

Source: Internet
Author: User
Tags scalar

The following examples for you to explain more intuitive, more convenient for everyone to understand.

I call webservice through Jquery.ajax in my project.

The client code is as follows:

$.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");
}

The service-side code is as follows:

[WebMethod]
Public XmlDocument xxx (string name, string [] tags)
{return 
sth; 
}

Always throws an exception.

The problem appears here:

The following is the HTTP data:

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
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

And the format it expects is the following:

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

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 the problem is on the Jquery.ajax. See Code (JQUERY.1.8.3.JS)

function buildparams (prefix, obj, traditional, add) {var name; if (Jquery.isarray (obj)) {//Serialize array item. jque Ry.each (obj, function (i, v) {if (traditional | | rbracket.test (prefix)) {//Treat each array item as a scalar. Add (prefi
x, v); else {//If array item is non-scalar (array or object), encode it//numeric index to resolve deserialization ambiguit
Y issues.  Note that rack (as of ...) can ' t currently deserialize//nested arrays properly, and attempting to did so may cause//a Server error. Possible fixes are to modify rack ' s//deserialization algorithm or to provide a option or flag/to force array Seriali
Zation 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 (name in obj) {Buildparam s (prefix + "[" + Name + "]", obj[name], traditional, add);} Else {//Serialize scalar item. Add (prefix, obj);}} 

Conclusion:

The offending code is on line 22, and I'll change it to 21 lines.

However, I js/jquery are a smattering of, hope not to cause other sequelae, hehe.

The above is a small set to introduce the jquery through the AJAX call WebService Pass array parameters of the problem examples of the relevant knowledge, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.