Javascript Angular:将参数传递给 $http.get
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32799095/
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
Angular: Pass params to $http.get
提问by realph
I'm trying to pass a params object to the $http.get()
service. My params look like this:
我正在尝试将 params 对象传递给$http.get()
服务。我的参数如下所示:
var params = {
one: value,
two: value
}
And I'm trying to pass them into my function like so:
我试图将它们传递到我的函数中,如下所示:
$http.get('/someUrl', params)
.success(function(data) {
// stuff
})
.error(function(data) {
// error stuff
});
Is this the correct way to go about doing this?
这是执行此操作的正确方法吗?
回答by Michael P. Bazos
The second argument of $http
is a configobject (see documentation). Amongst other properties, the configobject accepts a params
property:
的第二个参数$http
是一个配置对象(参见文档)。在其他属性中,配置对象接受一个params
属性:
- params –
{Object.<string|Object>}
– Map of strings or objects which will be serialized with the paramSerializer and appended as GET parameters.
- params –
{Object.<string|Object>}
– 将使用 paramSerializer 序列化并附加为 GET 参数的字符串或对象的映射。
Therefore you have to pass the parameters as such
因此,您必须像这样传递参数
var config = {
params: {
one: value,
two: value
}
}
$http.get('/someUrl', config).then(...)
Suppose the values for the parameters are respectively '1' and '2', $http
will send a GET request to the following url:
假设参数的值分别为“1”和“2”,$http
将向以下 url 发送 GET 请求:
/someUrl?one=1&two=2
As a side note, try to avoid using success
and error
functions on $http
. They have been deprecated as of angular 1.4.4. Use the methods then
with a success and an error callback instead, or then with only a success callback and catch
.
作为旁注,尽量避免在 上使用success
和error
函数$http
。从 angular 1.4.4 开始,它们已被弃用。使用then
具有成功和错误回调的方法,或者只使用成功回调和catch
.
回答by Joe Lloyd
Service/Factory
服务/工厂
For the actual call use a factory or service that you can inject to the controllers you need it in. This is an example factory passing parameters
对于实际调用,请使用工厂或服务,您可以将其注入到需要它的控制器中。这是工厂传递参数的示例
.factory('Chats', function ($http, $rootScope, $stateParams) {
return {
all: function () {
return $http.get('http://ip_address_or_url:3000/chats', { params: { user_id: $rootScope.session } })
}
};
});
Controller
控制器
In your controller you use the service like this
在您的控制器中,您使用这样的服务
.controller('ChatsCtrl', function ($scope, Chats) {
Chats.all().success(function (response) {
$scope.chats = response;
})
})
回答by Shrikant
I have faced similar issue in recent time and I had to add few additional details to request (I used accepted answer with some headers):
我最近遇到了类似的问题,我不得不向请求添加一些额外的细节(我使用了一些标题的接受答案):
$http.get(url, {
params: {
paramOne: valueOne,
paramTwo: valueTwo,
...
},
headers: {
'key': 'value'
},
// responseType was required in my case as I was basically
// retrieving PDf document using this REST endpoint
// This is not required in your case,
// keeping it for somebody else's reference
responseType: 'arraybuffer'
}).success(
function(data, status, headers, config) {
// do some stuff here with data
}).error(function(data) {
// do some stuff here with data
});
回答by Shikloshi
The $http documentationsuggest that the second argument to the $http.get method is an object which you can pass with it "param" object.
$http文档建议 $http.get 方法的第二个参数是一个对象,您可以将其传递给“param”对象。
Try something like this:
尝试这样的事情:
$http.get('/someUrl', {params: params})
.success(function(data) {
// stuff
})
.error(function(data) {
// error stuff
});