如何使用AngularJS中的一些数据进行$ http GET? [英] How to do a $http GET with some data in AngularJS?
问题描述
我想向我的后端发出Get请求,但是我想在发送响应对象之前验证一些用户凭据.
I want to make a Get request to my Backend, but I want to verify some user credentials before sending the response object.
这是我的代码:
$scope.getEverything = function (){
$http.get('http://localhost:1234/things').success(function(data){
$scope.things = data;
});
};
我用以下语法尝试了相同的代码:
I've tried the same code with the following syntax:
$http.method('path).then
$http.method(config)
我只是不知道如何传递一些数据.尝试使用params:data作为配置对象,并打印输出:
And I just could not figure out how to pass in some data. Trying to use params:data as the config object with print out:
GET http://localhost:1234/[object%20Object] 404(不找到)
GET http://localhost:1234/[object%20Object] 404 (Not Found)
在控制台上.
AngularJS文档只字未提使用GET请求发送数据.我也尝试过一些Stack的答案,但是其中大多数已经过时了,将无法使用.
The AngularJS Docs don't say a word about sending data with GET requests. I've also tried some answers of Stack, but most of then are outdated and just won't work.
我可以使用GET请求发送数据,还是这只是我的应用程序的一个设计错误?
Can I send data with GET request or this is just one design error of my application?
推荐答案
您最有可能需要使用POST方法而不是GET方法.
You're most likely going to need to use a POST method instead of a GET method.
但是要使用GET方法:
But to do it with a GET method:
从 AngularJS将数据传递到$ http.get请求:
HTTP GET请求不能包含要发布到服务器的数据. 但是,您可以将查询字符串添加到请求中.
A 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为它的参数提供了一个选项.
angular.http provides an option for it params.
$http({
url: user.details_path,
method: "GET",
params: {user_id: user.id}
});
并使用 AngularJS 进行POST:
$http.post('/someUrl', {msg:'hello word!'}).
then(function(response) {
// this callback will be called asynchronously
// when the response is available
}, function(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
这篇关于如何使用AngularJS中的一些数据进行$ http GET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!