How to add multiple same-name query parameters in

2019-04-20 06:07发布

All

What I want to do is Adding multiple parameters with same name to request URL with AngularJS $http, making the URL sent like http://localhost:8080/?value=1&value=2.....

This post also described the similar situation: Sending HTTP request with multiple parameters having same name

Could any one help with in AngularJS $http?

Thanks

2条回答
Luminary・发光体
2楼-- · 2019-04-20 06:25

You call also use URLSearchParams for the parameters. For example:

let params = new URLSearchParams();

params.append('someParmName', 'value1');
params.append('someParmName', 'value2');

// the following statement will form this url: ?someParmName=value1&someParmName=value2
http.get(api_url, {search: params});

Or

let params = new URLSearchParams();

params.set('someParmName', ['value1', 'value2'];);

// the following statement will form this url: ?someParmName=value1,value2
http.get(api_url, {search: params});
查看更多
放荡不羁爱自由
3楼-- · 2019-04-20 06:34

You need to use an array like this:

 var params = {};
 params.someParmName = ['value1', 'value2'];

This will send value1 and value2 with the same querystring key 'someParmName'.

Here is an example GET request:

 var parameters = {};
 parameters.someParmName = ['value1', 'value2'];

 $http({
    url: 'api_url', 
    method: "GET",
    params: parameters
});
查看更多
登录 后发表回答