我已經將我的Express JS API部署到了Heroku這里:
<https://mylink.herokuapp.com/somefile/do-somestuff
我把我的React JS應用放在http://localhost:3000
當我使用PostMan消耗它時,Heroku API作業得非常好。
localhost/:1 來自'https://mylink.herokuapp.com/somefile/do-somestuff'的XMLHttpRequest的訪問已被CORS策略所阻止。請求的資源上沒有'Access-Control-Allow-Origin'頭。
mylink.herokuapp.com/somefile/do-somestuff:1 Failed to load resource: net::ERR_FAILED 這里是我的Express JS API: 在someFile.js中: 下面是我的React代碼: 問題是什么,為什么在從React JS中消耗heroku api時不能作業,而且總是拋出以下錯誤: localhost/:1 來自'https://mylink.herokuapp.com/somefile/do-somestuff'的XMLHttpRequest的訪問被CORS策略阻止。請求的資源上沒有'Access-Control-Allow-Origin'頭。
mylink.herokuapp.com/somefile/do-somestuff:1 Failed to load resource: net::ERR_FAILED uj5u.com熱心網友回復: 你可能需要修改客戶端的axios請求頭來設定 回應代碼: 我知道你可能已經知道了,但請確保你在運行后端時取消注釋。看起來這個設定不錯: 編輯:我收到了你的通知,但我猜你洗掉了評論。請記住,如果你沒有在其他地方設定axios請求物件的頭資訊,你可能必須這樣做,以便通過cors pre-flight請求。我相信你不需要在axios請求中添加 因此,如果你在瀏覽器開發工具中的網路選項卡中檢查請求,并且你得到了某些東西,比如-- 那么要么是路由、應用程式或 cors 模塊的設定不正確。也許你忘了取消對你送入 cors 模塊的頭檔案物件的注釋?如果你為Heroku設定了一個源代碼管道,請檢查該分支中的檔案,因為這將是在Heroku實體上執行的代碼,看看一切是否設定得正確。
uj5u.com熱心網友回復: 你必須創建一個模式,以便你可以將資料發送到后端前端,并嘗試重新安裝CORS。
CORS使你能夠從不同的來源訪問一個資源。它是用來覆寫你的瀏覽器由于SOP而產生的默認行為的。因此,現在當你的客戶端請求一個資源時,回應將另外包含一個戳記,告訴你的瀏覽器允許在不同的起源之間共享資源。
標籤:const express = require('express')。
const app = express()。
const myRouter = require('./someFile') 。
const cors = require('cors') 。
app.listen(process.env.PORT, () => {
console.log(`App is listening on port 5000`)。
});
app.use(cors(/**{
origin: `http://localhost:3000`, //react的地址
憑證: true
}*/))。)
app.use(express.json()。
app.use('/somefile', someFile)。
app.get('/', (request, response) => {
response.send('Homepage') 。
});
module.exports = app;
const express = require('express') 。
const app = express()。
const router = express.Router();
router.post('/do-somestuff', async(request, response)=>{
let token = request.body.passedtoken;
let latestOTP = await otpCodeModel.find({someField:token}) 。 sort({_id: -1})。limit(1)。
return response.json({message: latestOTP})。
});
module.exports = router;
class App extendsReact.Component{
constructor(props){
super(props)。
}
sendPassedToken = (token) => {
axios.post(`https://mylink.herokuapp.com/somefile/do-somestuff`, token)
.then((response) => {
console.log(response.data.message)。
}).catch((error) =>/span> {
console.log(error)。
});
console.log(token)。
}
}
export default App;
withCredentials: true和/或credentials: 'include'。class App extendsReact.Component{
constructor(props){
super(props)。
}
sendPassedToken = (token) => {
軸心
.post(
`https://mylink.herokuapp.com/somefile/do-somestuff`,
token,
{ withCredentials: true, credentials: 'include' }
)
.then((response) =>/span> {
console.log(response.data.message)。
}).catch((error) =>/span> {
console.log(error)。
});
console.log(token)。
}
}
export default App;
app.use(cors({
origin: `http://localhost:3000`, //react的地址。
credentials: true
credentials: 'include'(這可能是為了獲取),但你需要為axios添加withCredentials: true,除非你在其他地方對axios物件本身進行了設定。這種型別的cors設定需要你指定一個起源,而不是 "*",因為你要設定credentials:true,而你是在后端進行的。
當憑證標志為真時,通配符'*'不能在'Access-Control-Allow-Origin'頭中使用。因此,起源'http://localhost:3010'不允許訪問。
