Angular - Set headers for every request

前端 未结 19 2004
[愿得一人]
[愿得一人] 2020-11-22 07:43

I need to set some Authorization headers after the user has logged in, for every subsequent request.


To set headers for a particular request,



        
19条回答
  •  甜味超标
    2020-11-22 08:27

    I has able to choose a simplier solution > Add a new Headers to the defaults options merge or load by your api get (or other) function.

    get(endpoint: string, params?: any, options?: RequestOptions) {
      if (!options) {
        options = new RequestOptions();
        options.headers = new Headers( { "Accept": "application/json" } ); <<<<
      }
      // [...] 
    }
    

    Of course you can externalize this Headers in default options or whatever in your class. This is in the Ionic generated api.ts @Injectable() export class API {}

    It is very quick and it work for me. I didn't want json/ld format.

提交回复
热议问题