本文翻译自:AngularJS passing data to $http.get request
I have a function which does a http POST request. 我有一个执行http POST请求的函数。 The code is specified below. 该代码在下面指定。 This works fine. 这很好。
$http({
url: user.update_path,
method: "POST",
data: {user_id: user.id, draft: true}
});
I have another function for http GET and I want to send data to that request. 我为http GET提供了另一个功能,我想将数据发送到该请求。 But I don't have that option in get. 但是我没有那个选择。
$http({
url: user.details_path,
method: "GET",
data: {user_id: user.id}
});
The syntax for http.get
is http.get
的语法是
get(url, config) 获取(URL,配置)
#1楼
参考:https://stackoom.com/question/vjcc/AngularJS将数据传递到-http-get请求
#2楼
An HTTP GET request can't contain data to be posted to the server. HTTP GET请求不能包含要发布到服务器的数据。 However, you can add a query string to the request. 但是,您可以将查询字符串添加到请求中。
angular.http provides an option for it called params
. angular.http为此提供了一个名为params
的选项。
$http({
url: user.details_path,
method: "GET",
params: {user_id: user.id}
});
See: http://docs.angularjs.org/api/ng.$http#get and https://docs.angularjs.org/api/ng/service/$http#usage (shows the params
param) 请参阅: http : //docs.angularjs.org/api/ng.$http#get和https://docs.angularjs.org/api/ng/service/$http#usage (显示params
参数)
#3楼
You can pass params directly to $http.get()
The following works fine 您可以将参数直接传递给$http.get()
,以下方法可以正常工作
$http.get(user.details_path, {
params: { user_id: user.id }
});
#4楼
You can even simply add the parameters to the end of the url: 您甚至可以简单地将参数添加到url的末尾:
$http.get('path/to/script.php?param=hello').success(function(data) {
alert(data);
});
Paired with script.php: 与script.php配对:
<? var_dump($_GET); ?>
Resulting in the following javascript alert: 导致以下JavaScript警报:
array(1) {
["param"]=>
string(4) "hello"
}
#5楼
Solution for those who are interested in sending params and headers in GET request 对于那些对在GET请求中发送参数和标头感兴趣的人的解决方案
$http.get('https://www.your-website.com/api/users.json', {
params: {page: 1, limit: 100, sort: 'name', direction: 'desc'},
headers: {'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='}
}
)
.then(function(response) {
// Request completed successfully
}, function(x) {
// Request error
});
Complete service example will look like this 完整的服务示例如下所示
var mainApp = angular.module("mainApp", []);
mainApp.service('UserService', function($http, $q){
this.getUsers = function(page = 1, limit = 100, sort = 'id', direction = 'desc') {
var dfrd = $q.defer();
$http.get('https://www.your-website.com/api/users.json',
{
params:{page: page, limit: limit, sort: sort, direction: direction},
headers: {Authorization: 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='}
}
)
.then(function(response) {
if ( response.data.success == true ) {
} else {
}
}, function(x) {
dfrd.reject(true);
});
return dfrd.promise;
}
});
#6楼
Starting from AngularJS v1.4.8 , you can use get(url, config)
as follows: 从AngularJS v1.4.8开始,您可以如下使用get(url, config)
:
var data = {
user_id:user.id
};
var config = {
params: data,
headers : {'Accept' : 'application/json'}
};
$http.get(user.details_path, config).then(function(response) {
// process response here..
}, function(response) {
});