通过GET请求发送阵列AngularJS'$ http服务 [英] Send array via GET request with AngularJS' $http service

查看:150
本文介绍了通过GET请求发送阵列AngularJS'$ http服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要发送使用 $ HTTP 服务GET请求。参数之一将是id的阵列。该网址看起来像这样的 mysite.com/items?id [] = 1和ID [] = 2和ID [] = 3和ID [] = 4

我试过这个方法

$ HTTP(
  方法:GET,
  网址:'/项目',
  params:一个{
    ID:IDS // IDS是[1,2,3,4]
  }

但我obain URL为 mysite.com/items?id=%5B%221%22%2C%222%22%2C%223%22%2C%224%22%5D

这是因为角度是在一个JSON字符串转换我的价值。有没有办法来得到我想要的行为?

[更新]

我解决使用问题,由于乔纳森的建议jQuery的 $。参数()

$ HTTP(
  方法:GET
  网址:'/项目? + $ .PARAM({ID:IDS})


解决方案

  $ HTTP(
  方法:GET,
  网址:'/项目',
  params:一个{
    编号:JSON.stringify(IDS)// IDS是[1,2,3,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

I tried this approach

$http(
  method: 'GET',
  url: '/items',
  params: {
    id: ids // ids is [1, 2, 3, 4]
  }
)

but the url I obain is mysite.com/items?id=%5B%221%22%2C%222%22%2C%223%22%2C%224%22%5D

That's Because Angular is converting my value in a JSON string. Is there a way to get the behavior I want?

[Update]

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]
  }
)

这篇关于通过GET请求发送阵列AngularJS'$ http服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆