代码之家  ›  专栏  ›  技术社区  ›  IsaacK

未捕获的类型错误:无法在httpxhrbackend处读取未定义的属性“method”

  •  0
  • IsaacK  · 技术社区  · 7 年前

    我正在遵循关于使用AngularHTTPInterceptor的教程,但是当我调用handle()方法时,会得到一个错误。

    //下一个.handle();

    Uncaught TypeError: Cannot read property 'method' of undefined at HttpXhrBackend.push../node_modules/@angular/common/fesm5/http.js.HttpXhrBackend.handle (http://localhost:4200/vendor.js:31148:17) at eval (eval at push../src/app/services/authInterceptor.service.ts.AuthInterceptorService.intercept (http://localhost:4200/main.js:756:9), <anonymous>:1:6) at AuthInterceptorService.push../src/app/services/authInterceptor.service.ts.AuthInterceptorService.intercept (http://localhost:4200/main.js:756:9) at HttpInterceptorHandler.push../node_modules/@angular/common/fesm5/http.js.HttpInterceptorHandler.handle (http://localhost:4200/vendor.js:30859:33) at HttpXsrfInterceptor.push../node_modules/@angular/common/fesm5/http.js.HttpXsrfInterceptor.intercept (http://localhost:4200/vendor.js:31450:25) at HttpInterceptorHandler.push../node_modules/@angular/common/fesm5/http.js.HttpInterceptorHandler.handle (http://localhost:4200/vendor.js:30859:33) at HttpInterceptingHandler.push../node_modules/@angular/common/fesm5/http.js.HttpInterceptingHandler.handle (http://localhost:4200/vendor.js:31494:27) at MergeMapSubscriber.project (http://localhost:4200/vendor.js:30699:184) at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._tryNext (http://localhost:4200/vendor.js:143493:27) at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._next (http://localhost:4200/vendor.js:143483:18)
    
    
    import { Injectable } from '@angular/core';
    import { HttpInterceptor } from '@angular/common/http';
    
    @Injectable()
    export class AuthInterceptorService implements HttpInterceptor{
      intercept(req,next){
        console.log(req);
        return next.handle();
      }
    
    }
    

    我做错什么了吗?,因为请求记录正常。

    我使用的是角版本7.0.3

    2 回复  |  直到 7 年前
        1
  •  1
  •   Sheik Althaf    7 年前

    请发送 req 把手

    intercept(req,next){
       console.log(req);
       return next.handle(req); <--- here
    }
    
        2
  •  1
  •   Farhat Zaman    7 年前

    试试这个

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        console.log(req);
        return next.handle(req);
      }
    
    推荐文章