AngularJS 将数据传递给 $http.get 请求 [英] AngularJS passing data to $http.get request
问题描述
我有一个执行 http POST 请求的函数.代码在下面指定.这工作正常.
I have a function which does a http POST request. The code is specified below. This works fine.
$http({
url: user.update_path,
method: "POST",
data: {user_id: user.id, draft: true}
});
我有另一个用于 http GET 的函数,我想向该请求发送数据.但是我在 get 中没有那个选项.
I have another function for http GET and I want to send data to that request. But I don't have that option in get.
$http({
url: user.details_path,
method: "GET",
data: {user_id: user.id}
});
http.get
的语法是
get(url, config)
推荐答案
HTTP GET 请求不能包含要发布到服务器的数据.但是,您可以向请求添加查询字符串.
An HTTP GET request can't contain data to be posted to the server. However, you can add a query string to the request.
angular.http 为其提供了一个名为 params
的选项.
angular.http provides an option for it called params
.
$http({
url: user.details_path,
method: "GET",
params: {user_id: user.id}
});
参见:http://docs.angularjs.org/api/ng.$http#get 和 https://docs.angularjs.org/api/ng/service/$http#usage(显示 params
参数)
See: http://docs.angularjs.org/api/ng.$http#get and https://docs.angularjs.org/api/ng/service/$http#usage (shows the params
param)
这篇关于AngularJS 将数据传递给 $http.get 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!