使用 AngularJS 的 $http 服务通过 GET 请求发送数组 [英] Send array via GET request with AngularJS' $http service
本文介绍了使用 AngularJS 的 $http 服务通过 GET 请求发送数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用 $http
服务发送 GET 请求.其中一个参数是一个 id 数组.网址看起来像这样 mysite.com/items?id[]=1&id[]=2&id[]=3&id[]=4
I need to send a GET request using the $http
service. One of the parameters will be an array of ids. The url looks like this one mysite.com/items?id[]=1&id[]=2&id[]=3&id[]=4
我试过这种方法
$http(
method: 'GET',
url: '/items',
params: {
id: ids // ids is [1, 2, 3, 4]
}
)
但我获得的网址是 mysite.com/items?id=%5B%221%22%2C%222%22%2C%223%22%2C%224%22%5D
那是因为 Angular 正在将我的值转换为 JSON 字符串.有没有办法获得我想要的行为?
That's Because Angular is converting my value in a JSON string. Is there a way to get the behavior I want?
[更新]
多亏乔纳森的建议,我使用 jQuery 的 $.param()
解决了这个问题.
I solved the issue thanks to Jonathan's suggestion using jQuery's $.param()
.
$http(
method: 'GET'
url: '/items?' + $.param({id: ids})
)
推荐答案
$http(
method: 'GET',
url: '/items',
params: {
id: JSON.stringify(ids) // ids is [1, 2, 3, 4]
}
)
这篇关于使用 AngularJS 的 $http 服务通过 GET 请求发送数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文