Home > Web Front-end > JS Tutorial > The problem of passing array parameters through ajax calling webservice in jQuery (graphic tutorial)

The problem of passing array parameters through ajax calling webservice in jQuery (graphic tutorial)

亚连
Release: 2018-05-22 11:54:38
Original
2036 people have browsed it

This article will give you a detailed introduction to jQuery through ajax to call webservice to pass array parameters through examples. Friends who need it can refer to

The following examples will be used to explain it to you more intuitively and easier for everyone to understand.

In my project, I call webservice through jquery.ajax.

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");
}
});
Copy after login

The server code is as follows:

[WebMethod]
public XmlDocument xxx(string name, string [] tags )
{ 
return sth; 
}
Copy after login

Always throws an exception.

The problem arises 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
Content-Type: application/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
Copy after login

And the format it expects is As follows:

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
Copy after login

Compare the bold font above, the post data except the problem. The correct one should be as follows:

name=zhangsan&tags=aa&tags=bb&tags=cc
Copy after login

It seems that the problem lies in jquery.ajax. See the code (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't currently deserialize
// nested arrays properly, and attempting to do so may cause
// a server error. Possible fixes are to modify rack's
// 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);
}
}
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Detailed explanation of the method of using ajax to pass arrays and receive in the background

Simple implementation of AJAX paging effect (graphic tutorial)

How to solve the problem of Ajax passing data with special characters

The above is the detailed content of The problem of passing array parameters through ajax calling webservice in jQuery (graphic tutorial). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template