我如何获得$ http readyState,像这样使用:
var request, interval;
request = $http
.post('/user/info', {...})
.success(...);
interval = setInterval(function(){
if(request.readyState < 3)
return;
prepare(request.data, request.headers);
clearInterval(interval)
}, 10);
function prepare(data, headers){
...
}
我不知道如何在不更改angular.js文件的情况下执行此操作。是否可以通过$ httpBackend或其他方式向服务中添加一些功能?
使用AngularJS 1.5.5,添加了对XHR事件的其他处理的支持:
$ http参数-配置
描述要发出的请求及其处理方式的对象。该对象具有以下属性:
- 事件处理器-
{Object}
-事件监听器绑定到XMLHttpRequest对象。要将事件绑定到上XMLHttpRequest
载对象,请使用uploadEventHandlers
。该处理程序将在一个$apply
块的上下文中被调用。- uploadEventHandlers -
{Object}
-事件侦听器绑定到了XMLHttpRequest上传对象。要将事件绑定到XMLHttpRequest
对象,请使用eventHandlers。该处理程序将在一个$apply
块的上下文中被调用。
使用eventHandlers
config对象的属性添加一个获取XHR 的事件处理程序readyState
:
angular.module("app",[])
.run(function($rootScope, $http){
var eventHandlers = {readystatechange: readyStateChangeHandler};
var config = { eventHandlers: eventHandlers };
$rootScope.messageList = [];
function readyStateChangeHandler(ev) {
var message = "readyState: "+ev.target.readyState;
console.log(message);
$rootScope.messageList.push(message);
}
$http.get("//httpbin.org/anything",config)
.then(function(response){
console.log("OK");
//console.log(response);
}).catch(function(response){
console.log("ERROR");
//console.log(response);
})
})
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="app">
<div ng-repeat="m in messageList">
{{m}}
</div>
</body>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句