且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

RxJs 如何设置默认请求头?

更新时间:2022-03-16 04:51:49

不可能使用 RxJS 的 ajax 实用程序为所有 ajax 请求设置默认标头.

It's not possible to set default headers for all ajax requests using RxJS's ajax utilities.

然而,您可以在每次调用中提供标头,或者创建自己的简单包装器来默认提供它们.

You can however provide headers in each call, or create your own simple wrapper that provides them by default.

const defaultHeaders = {
  Authorization: 'c7b9392955ce63b38cf090...etc'
};

export const get = (url, headers) =>
  ajax.get(url, Object.assign({}, defaultHeaders, headers));

my-example.js

import * as ajax from './utils/ajax';

// Usage is the same, but now with defaults
ajax.get(`http://localhost/products?${action.q}`;)