我有一个使用npm start运行的React项目,此代码从第二次读取中获得401错误(第一个正常)。它仅在节点返回200时运行良好,例如在“ node App.js”中。
所以我需要做什么来运行我的React项目以得到200个响应?为什么npm和此请求响应的节点之间存在这种差异?
const clientID = <ClientID>
const clientSecret = <ClientSecret>
const encode = Buffer.from(`${clientID}:${clientSecret}`, 'utf8').toString('base64')
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': `Basic ${encode}`,
},
};
fetch("https://auth-nato.auth.us-east-1.amazoncognito.com/oauth2/token?grant_type=client_credentials", requestOptions)
.then(response => { return response.json() })
.then(data => {
const requestOptions2 = {
method: 'POST',
mode: 'no-cors',
headers: { 'Content-Type': 'application/json',
'Authorization': `Bearer ${data.access_token}`
},
body: '{"username":"Ana", "password":"test123","user_id":"[email protected]"}'
};
fetch('https://j1r07lanr6.execute-api.sa-east-1.amazonaws.com/v1/register', requestOptions2)
.then(response => {console.log(response)});
})
唯一的解决方案是禁用chrome web安全性并删除“模式:no-cors”。我尝试将“ Access-Control-Allow-Origin”:“ http://localhost:3000”添加到标题中,但不起作用。