如何将Jquery ajax get params更改为ajax post params,例如“?name = 2& name = 3& name = 4"? [英] How to change Jquery ajax get params to ajax post params, like '?name=2&name=3&name=4'?
问题描述
我想在Jquery ajax中向服务器发布很多相同的字段值,我可以通过将所有值添加到url来做到这一点,但是我想知道如何使用data属性来做到这一点.必须有平等的方式.
I wanna post a lot of same field values to server in Jquery ajax, I can do it via adding all values to url, but I wanna know how to do it using data attribute. There must have an equal way.
$.ajax({
type : "POST",
url : 'abc.action?name=5&name=6',
data : {
name : 2,
name : 3
},
error : function() {
console.error("operate failed");
},
success : function(data) {
console.log(data);
}
});
在上面的代码中,请求发送5、6和3到服务器,但不发送2.
In above code, the request send 5, 6 and 3 to server, but not 2.
也许使用不同的服务器端此问题会有所不同,我使用的是Struts2.
Maybe this problem will be different using different server side, I use Struts2.
推荐答案
You seem to be looking for an array
.
只需将变量name
定义为数组.然后将所有值放入其中,一旦所有值都包含进来,就在ajax
请求中将其发送.
Just define a variable name
as an array. And push all the values into it, and once it has all the values then send it in the ajax
request.
name = [];
name.push("2");
name.push("3");
$.ajax({
type : "POST",
url : 'abc.action',
data : {
name : name
},
error : function() {
.....
这篇关于如何将Jquery ajax get params更改为ajax post params,例如“?name = 2& name = 3& name = 4"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!