我正在向請求兩個身份驗證標頭的 api 發出 GET 型別請求,其中一個稱為令牌,另一個稱為身份驗證。
我正在傳遞兩個必需的引數,如下面的列印螢屏所示:

作為回報,我收到錯誤訊息:
Access to XMLHttpRequest at 'https://pay.apiname.com.br/v1/sellers/d.....2/payments/list' from origin 'http://10.0.0.150:3006' has been blocked by CORS policy: Request header field token is not allowed by Access-Control-Allow-Headers in preflight response.
我正在使用 axios 發出請求。在下面的檔案中,您有此特定中間件的請求配置。我什至在必要時在 redux 操作中運行此請求。
paymentApi: {
client: axios.create({
baseURL: process.env.API,
responseType: 'json',
}),
options: {
returnRejectedPromiseOnError: true,
interceptors: {
request: [
({ getState }, config) => {
// here I look for the user information from the state, where it contains the authorization and token needed for the header.
const { auth } = getState().get('login').toJS();
return {
...config,
headers: {
...(config.headers || {}),
token: auth.user.token_parcela
? `${auth.user.token_parcela}`
: undefined,
Authorization: auth.user.api_key_parcela
? `${auth.user.api_key_parcela}`
: undefined,
Accept: 'application/json',
'Content-Type': 'application/json',
},
};
},
],
response: [
{
success: (store, response) => response,
error: (_store, error) => {
console.error(error);
return Promise.reject(error);
},
},
],
},
},
},
奇怪的是,如果您嘗試使用郵遞員發出此請求,我可以毫無問題地做到這一點,請參閱下面的列印螢屏。

uj5u.com熱心網友回復:
該問題與您的代碼無關,托管/構建它的 API 需要允許 CORS 源,并且您必須將呼叫此 API 的網站指定為源。
在此處閱讀有關 CORS 的更多資訊 - CORS
它也適用于 Postman 是因為 API 是直接呼叫的,而不是從任何源/來源/網站呼叫,而不是從您的代碼中呼叫。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/344107.html
