Javascript 如何扩展HttpRequest参数?

Javascript 如何扩展HttpRequest参数?,javascript,angular,typescript,web,Javascript,Angular,Typescript,Web,嗨,我是AngularJS,比AngularJS更新 AngularJS 在AngularJS$http服务中,我可以向特定http请求添加一些自定义配置: $http.post('api/data-source/item'.assign(opts), opts.parameters, **ignoreUnauthorized**).then(...) 与此RestAPI绕过全局http侦听器的ignoreUnauthorized选项类似: responseError: function

嗨,我是AngularJS,比AngularJS更新

  • AngularJS
在AngularJS$http服务中,我可以向特定http请求添加一些自定义配置:

 $http.post('api/data-source/item'.assign(opts), opts.parameters, **ignoreUnauthorized**).then(...)
与此RestAPI绕过全局http侦听器的
ignoreUnauthorized
选项类似:

responseError: function(response) {
    if (response.status === 401 && !response.config.**ignoreUnauthorized**) {
        $rootScope.$broadcast('error:unauthorized', response);
        return $q.defer().promise;
    }
    return $q.reject(response);
}
  • 角度4.3+
但是在Angular HttpClient中,我们不能将这样的属性添加到选项中,因为TypeScript从不允许我这样做。 我还尝试扩展
HttpClient
,以添加额外的选项:

export interface AppHttpParams {
    headers?: HttpHeaders;
    observe?: 'body';
    params?: HttpParams;
    reportProgress?: boolean;
    responseType?: 'json';
    withCredentials?: boolean;
    body?: any;
    **ignoreUnauth**: boolean;
}

export function applicationHttpClientCreator(http: HttpClient) {
    return new AppHttpClient(http);
}

@Injectable()
export class AppHttpClient {
    public constructor(public http: HttpClient) {}
    ...
}
但是定制选项ignoreUnauth似乎不是随HttpRequest对象提供的,HttpRequest对象中的所有属性似乎都是默认值(从Chrome控制台输出):


有没有办法在AngularJS中向
HttpRequest
添加我自己的选项?或者有其他方法来扩展
HttpRequest
选项?

我正在使用http头来实现这一点

只需将所需值设置为自定义http标头:

new HttpHeaders().set("X-my-custom-header", string | string[]);
然后您可以在http拦截器中删除它们(以避免公共可见性)


只需将参数作为字符串传递,这应该可以正常工作
this.http.get('localhost',{params:{'ignoreUnauth':'true'}).subscribe()您是否在lssbq找到了解决方案?
new HttpHeaders().set("X-my-custom-header", string | string[]);
intercept(
    req: HttpRequest<any>,
    next: HttpHandler
  ): Observable<HttpEvent<any>> {
    const request = req.clone({
      headers: req.headers
                 .delete("X-my-custom-header")
    });
    return next.handle(request).pipe(
      catchError(err => this.handleHttpError(err, req))
    );
  }
const myCustomHeaderValue = req.headers.getAll("X-my-custom-header");