I'm trying to GET some data from a site using jQuery $.get. I need to set 2 parameters of the same type:
我正在尝试使用jQuery $. GET从站点获取一些数据。我需要设置2个相同类型的参数:
..&q=Some Text&q=Some other text
jQuery appears to be overwriting the first instance of q with the second and only sending 1. Any way around this?
jQuery似乎用第二个实例覆盖了第一个q实例,只发送了一个。解决这个问题的办法吗?
This is the code I was trying to use:
这就是我试图使用的代码:
var params = {
"otherParam":"x",
"q":text,
"q":title
};
$.get(url, params, mySuccessFunction);
5 个解决方案
#1
25
Try:
试一试:
var params = {
"otherParam": "x",
"q": [ text, title ]
};
edit — more information: array-valued parameters like that are treated specially by jQuery. To appease many common server frameworks, by default (since release 1.5 or 1.6 I think) those will result in parameter names that include "[]" (open- and close-bracket characters) as a suffix (no number, contrary to my erroneous comment below). If you don't want that, you can set
编辑-更多信息:像这样的arrayvalue参数是由jQuery专门处理的。为了满足许多常见的服务器框架,默认情况下(我认为是1.5或1.6版本),这些框架会导致参数名包括“[]”(开括号和右括号字符)作为后缀(没有数字,与我下面的错误注释相反)。如果你不想这样,你可以设置
jQuery.ajaxSettings.traditional = true;
and it'll just be "q" instead of "q[]".
它会是“q”而不是“q[]”。
#2
10
And another way to solve it that does not require encodeURIComponent()
or messing with jQuery.ajaxSettings.traditional = true;
which I would prefer not to do because I don't want to interfere (even temporarily) with what other parts of the site might be doing simultaneously.
另一种不需要encodeURIComponent()或使用jQuery.ajaxSettings的解决方法。传统= true;我宁愿不这样做,因为我不想(甚至是暂时地)干扰站点的其他部分可能同时进行的操作。
Is:
是:
var params=[
{name:"q", value:title},
{name:"q", value:text}
];
$.get(url, params, mySuccessFunction);
#3
5
Another approach without modifying jQuery.ajaxSettings.traditional = true;
is to use $.param()
http://api.jquery.com/jQuery.param/
不修改jQuery.ajaxSettings的另一种方法。传统= true;是要使用$.param() http://api.jquery.com/jQuery.param/
So something like this:
所以这样的:
var params = {
"otherParam": "x",
"q": [ text, title ]
};
$.get(url, $.param(params, true), mySuccessFunction);
#4
1
You can write the URL like this :
你可以这样写URL:
$.get(
url+'?q='+encodeURIComponent(text)+'&q='+encodeURIComponent(title)+'&otherParam='+encodeURIComponent('x'),
mySuccessFunction
);
#5
-1
Things are pretty simple and described in jQuery website http://api.jquery.com/jquery.param/.
jQuery网站http://api.jquery.com/jquery.param/描述的非常简单。
First create a params object
首先创建一个params对象
var params=[{
name: 'param1',
value: 'hello'
},
{
name: 'param1',
value: 'alex'
}];
next create the data to be sent to the service.
接下来创建要发送到服务的数据。
var data = jQuery.param(params);
That object can be inserted as the data param of the ajax request. I like using a settings object
该对象可以作为ajax请求的数据param插入。我喜欢使用settings对象
var settings={
url: 'api/domain/timeline',
type: 'get',
contentType: 'application/json',
data: data
};
$.ajax(settings);
#1
25
Try:
试一试:
var params = {
"otherParam": "x",
"q": [ text, title ]
};
edit — more information: array-valued parameters like that are treated specially by jQuery. To appease many common server frameworks, by default (since release 1.5 or 1.6 I think) those will result in parameter names that include "[]" (open- and close-bracket characters) as a suffix (no number, contrary to my erroneous comment below). If you don't want that, you can set
编辑-更多信息:像这样的arrayvalue参数是由jQuery专门处理的。为了满足许多常见的服务器框架,默认情况下(我认为是1.5或1.6版本),这些框架会导致参数名包括“[]”(开括号和右括号字符)作为后缀(没有数字,与我下面的错误注释相反)。如果你不想这样,你可以设置
jQuery.ajaxSettings.traditional = true;
and it'll just be "q" instead of "q[]".
它会是“q”而不是“q[]”。
#2
10
And another way to solve it that does not require encodeURIComponent()
or messing with jQuery.ajaxSettings.traditional = true;
which I would prefer not to do because I don't want to interfere (even temporarily) with what other parts of the site might be doing simultaneously.
另一种不需要encodeURIComponent()或使用jQuery.ajaxSettings的解决方法。传统= true;我宁愿不这样做,因为我不想(甚至是暂时地)干扰站点的其他部分可能同时进行的操作。
Is:
是:
var params=[
{name:"q", value:title},
{name:"q", value:text}
];
$.get(url, params, mySuccessFunction);
#3
5
Another approach without modifying jQuery.ajaxSettings.traditional = true;
is to use $.param()
http://api.jquery.com/jQuery.param/
不修改jQuery.ajaxSettings的另一种方法。传统= true;是要使用$.param() http://api.jquery.com/jQuery.param/
So something like this:
所以这样的:
var params = {
"otherParam": "x",
"q": [ text, title ]
};
$.get(url, $.param(params, true), mySuccessFunction);
#4
1
You can write the URL like this :
你可以这样写URL:
$.get(
url+'?q='+encodeURIComponent(text)+'&q='+encodeURIComponent(title)+'&otherParam='+encodeURIComponent('x'),
mySuccessFunction
);
#5
-1
Things are pretty simple and described in jQuery website http://api.jquery.com/jquery.param/.
jQuery网站http://api.jquery.com/jquery.param/描述的非常简单。
First create a params object
首先创建一个params对象
var params=[{
name: 'param1',
value: 'hello'
},
{
name: 'param1',
value: 'alex'
}];
next create the data to be sent to the service.
接下来创建要发送到服务的数据。
var data = jQuery.param(params);
That object can be inserted as the data param of the ajax request. I like using a settings object
该对象可以作为ajax请求的数据param插入。我喜欢使用settings对象
var settings={
url: 'api/domain/timeline',
type: 'get',
contentType: 'application/json',
data: data
};
$.ajax(settings);