我所熟悉的 jQuery的AJAX调用 ,其中有不同的回调像beforeSend,成功的,完整的,等等。
这是例子AJAX调用使用jQuery:
$。阿贾克斯({
网址:register.php,
键入:POST,
数据:{名称:姓名,电子邮件:电子邮件},
beforeSend:函数(){
//显示加载GIF
},
完成:函数(){
//隐藏加载GIF
},
成功:功能(数据){
//解析响应
}
});
我想用AngularJS来达到同样的
有没有类似的回调 beforeSend 作为AngularJS AJAX请求? 这是我的code,到目前为止,但我不知道我在哪里可以使用例如 beforeSend (这样我可以显示加载GIF图片)在我的code回调:
$ http.post('register.php',{'名':$ scope.name,电子邮件:$ scope.email})
.success(功能(数据,状态,头,配置){
如果(数据!=''){
}
});
解决方案
您可以使用拦截
。搜索词拦截
成角的 $ HTTP文档
作为文件说:为了进行全球性的错误处理,>认证,或任何形式的同步或异步pre-处理>请求或后处理的响应
修改
由于Satpal评论, $ httpProvider.responseInterceptors
在小提琴使用的是 pcated德$ P $
。您应该使用 $ httpProvider.interceptors
代替。
I am familiar with Jquery AJAX call, which has different callbacks like beforeSend, success, complete, etc.
This is the example AJAX call with Jquery:
$.ajax({
url: 'register.php',
type: 'POST',
data: {name:name, email:email},
beforeSend: function() {
// show loading GIF
},
complete: function() {
// hide loading GIF
},
success: function(data) {
// parse response
}
});
I want to achieve the same using AngularJS.
Is there a callback like beforeSend for AngularJS AJAX request ? This is my code so far, but i am not sure where can i use a callback like beforeSend (so that i can display a loading GIF image) in my code:
$http.post('register.php', {'name': $scope.name, 'email': $scope.email})
.success(function(data, status, headers, config) {
if (data != '') {
}
});
解决方案
You can use interceptors
. Search for the word interceptor
into the Angular $http documentation
As the documentation says : For purposes of global error handling, >authentication, or any kind of synchronous or asynchronous pre-processing of >request or postprocessing of responses
EDIT
As Satpal commented, $httpProvider.responseInterceptors
used in the Fiddle is deprecated
. You should use $httpProvider.interceptors
instead.