Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

javascript - AngularJS将数据传递到$ http.get请求(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,配置))

  ask by Sabarish Sankar translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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#gethttps://docs.angularjs.org/api/ng/service/$http#usage (显示params参数))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...