javascript 你如何将多个相同类型的参数传递给jQuery Get

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12876373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:19:10  来源:igfitidea点击:

How do you pass multiple parameters of the same type to jQuery Get

javascriptjquery

提问by Cliff Ribaudo

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 的第一个实例,并且只发送 1 个。有什么办法可以解决这个问题?

This is the code I was trying to use:

这是我试图使用的代码:

var params = {
       "otherParam":"x",
       "q":text,
       "q":title
};
$.get(url, params, mySuccessFunction);

回答by Pointy

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

编辑— 更多信息:像这样的数组值参数被 jQuery 特殊处理。为了安抚许多常见的服务器框架,默认情况下(我认为从 1.5 或 1.6 版开始)这些将导致参数名称包含“[]”(左括号和右括号字符)作为后缀(没有数字,与我的错误相反)下面评论)。如果你不想这样,你可以设置

jQuery.ajaxSettings.traditional = true;

and it'll just be "q" instead of "q[]".

它只会是“q”而不是“q[]”。

回答by Cliff Ribaudo

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.traditional = true;我不想做的事情,因为我不想(即使是暂时)干扰网站的其他部分可能同时做的事情。

Is:

是:

 var params=[ 
       {name:"q", value:title},
       {name:"q", value:text}
 ];
 $.get(url, params, mySuccessFunction);

回答by Jhony Fung

Another approach without modifying jQuery.ajaxSettings.traditional = true;is to use $.param()http://api.jquery.com/jQuery.param/

另一种无需修改的方法jQuery.ajaxSettings.traditional = 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);

回答by Denys Séguret

You can write the URL like this :

你可以这样写 URL:

$.get(
   url+'?q='+encodeURIComponent(text)+'&q='+encodeURIComponent(title)+'&otherParam='+encodeURIComponent('x'),
   mySuccessFunction
);

回答by Αλ?κο?

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 请求的数据参数插入。我喜欢使用设置对象

var settings={
    url: 'api/domain/timeline',
    type: 'get',
    contentType: 'application/json',
    data: data
    };
$.ajax(settings);