我想向我的后端发出Get请求,但我想在发送响应对象之前验证一些用户凭据.
这是我的代码:
$scope.getEverything = function (){
$http.get('http://localhost:1234/things').success(function(data){
$scope.things = data;
});
};
Run Code Online (Sandbox Code Playgroud)
我用以下语法尝试了相同的代码:
$http.method('path).then
$http.method(config)
Run Code Online (Sandbox Code Playgroud)
我只是无法弄清楚如何传递一些数据.尝试使用params:数据作为打印输出的配置对象:
获取http:// localhost:1234/[object%20Object] 404(未找到)
在控制台上.
AngularJS Docs没有说明使用GET请求发送数据.我也尝试了Stack的一些答案,但大部分时间已经过时而且无法正常工作.
我可以使用GET请求发送数据,或者这只是我的应用程序的一个设计错误?
您最有可能需要使用POST方法而不是GET方法.
但要使用GET方法:
HTTP GET请求不能包含要发布到服务器的数据.但是,您可以向请求添加查询字符串.
angular.http为它提供了一个选项.
$http({
url: user.details_path,
method: "GET",
params: {user_id: user.id}
});
Run Code Online (Sandbox Code Playgroud)
并使用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.
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
55460 次 |
| 最近记录: |