Questions tagged «interceptor»

3
如何拦截不同JS库发出的所有AJAX请求
我正在使用不同的JS库(AngularJS,OpenLayers等)构建一个Web应用程序,并且需要一种方法来拦截所有AJAX响应,以便在登录的用户会话过期(响应返回401 Unauthorized状态后)的情况下重定向他到登录页面。 我知道AngularJS提供interceptors了管理此类方案的方法,但是无法找到一种方法将这种注入OpenLayers请求中。因此,我选择了普通的JS方法。 在这里,我找到了这段代码... (function(open) { XMLHttpRequest.prototype.open = function(method, url, async, user, pass) { this.addEventListener("readystatechange", function() { console.log(this.readyState); // this one I changed }, false); open.call(this, method, url, async, user, pass); }; })(XMLHttpRequest.prototype.open); ...经过改编,看起来像预期的一样(仅在上一个Google Chrome上进行了测试)。 当它修改XMLHTTPRequest的原型时,我想知道这可能导致多么危险,或者是否会产生严重的性能问题。顺便说一句,还有其他有效的选择吗? 更新:如何在请求发送之前拦截请求 上一个技巧可以。但是,如果在同一场景中您想在发送请求之前注入一些标头,该怎么办?请执行下列操作: (function(send) { XMLHttpRequest.prototype.send = function(data) { // in this case I'm injecting …

1
将多个HTTP拦截器添加到Angular应用程序
如何将多个独立的HTTP拦截器添加到Angular 4应用程序? 我试图通过providers使用多个拦截器扩展数组来添加它们。但是只有最后一个被实际执行,才被Interceptor1忽略。 @NgModule({ declarations: [ /* ... */ ], imports: [ /* ... */ HttpModule ], providers: [ { provide: Http, useFactory: (xhrBackend: XHRBackend, requestOptions: RequestOptions) => new Interceptor1(xhrBackend, requestOptions), deps: [XHRBackend, RequestOptions], }, { provide: Http, useFactory: (xhrBackend: XHRBackend, requestOptions: RequestOptions) => new Interceptor2(xhrBackend, requestOptions), deps: [XHRBackend, RequestOptions] …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.