gpt4 book ai didi

RxJs 如何设置默认请求头?

转载 作者:行者123 更新时间:2023-12-05 09:18:03 25 4
gpt4 key购买 nike

不确定是否有任何方法可以像我们对 axios js 所做的那样在 rxjs 中设置默认请求 header 作为-

axios.defaults.headers.common['Authorization'] = 'c7b9392955ce63b38cf0901b7e523efbf7613001526117c79376122b7be2a9519d49c5ff5de1e217db93beae2f2033e9';

这是我的史诗代码,我想在其中设置请求 header -

export default function epicFetchProducts(action$, store) {
return action$.ofType(FETCH_PRODUCTS_REQUEST)
.mergeMap(action =>
ajax.get(`http://localhost/products?${action.q}`)
.map(response => doFetchProductsFulfilled(response))
);
}

请帮忙。

最佳答案

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

但是,您可以在每次调用中提供 header ,或者创建您自己的默认提供 header 的简单包装器。

utils/ajax.js

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

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

我的例子.js

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

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

关于RxJs 如何设置默认请求头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45778994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com