前后端分离容易导致跨域问题,所以前后端都要做相应设置。
前台
React 用axios withCredentials做如下设置:
axios.get(CONFIG.baseUrl + api, { params: data, withCredentials: true })
.then((res) => {
resolve(res)
})
.catch((err) => {
reject(err)
})
Angular 同样对 withCredentials做如下设置:
const clonedRequest = request.clone({
// headers: auth.getTokenHeader(),
withCredentials: true,
url: this.fixUrl(request.url)
});
后台
Node.js用cors组件,做如下配置
const cors = require('cors')
app.use(cors({credentials: true, origin: true}))
网友评论