перехватчики axios: пользовательский заголовок добавляется только в запрос OPTIONS - PullRequest
0 голосов
/ 30 апреля 2019

поэтому я установил перехватчик для axios, и я добавляю пользовательский заголовок к моим запросам.Тем не менее, когда я проверяю мой код сервера.Заголовок принимается только в запросе OPTIONS.

class ChainInterceptors {
  constructor(config) {
    this.cleanConfig = { ...config };
  }
  build() {
    return this.cleanConfig;
  }
}

export class ReqInterceptor extends ChainInterceptors {
  constructor(config) {
    super(config);
  }
  setLog() {
    this.cleanConfig.headers['Respond-By'] = // some value;
    return this;
  }
}

И я реализую его следующим образом:

export const request = {
  init() {
    const token = getItem('token');
    const request = axios.create({
      httpsAgent: new https.Agent({ keepAlive: true }),
      baseURL: LOCAL_URL,
      headers: {
        authorization: `bearer ${token}`,
        'Content-Type': 'application/json',
      },
    });
    this.attachInterceptor(request);
    return request;
  },

  attachInterceptor(request) {
    request.interceptors.request.use(config => {
      return new ReqInterceptor(config).setLog().build();
    });
    request.interceptors.response.use(
      response => {
        // response interceptors
      },
      error => {
        // Do something with response error
        return Promise.reject(error);
      }
    );
  },

  get() {
    return this.init().get.apply(null, arguments);
  },

  // and other methods...
};

Как я уже говорил выше, заголовок Respond-Byприсутствует только в моем запросе OPTIONS.Ваше руководство очень ценится.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...