邮递员中工作时出现 CORS 错误"/>
React js axios 在邮递员中工作时出现 CORS 错误
在我的 Mern Stack 项目中,当我从邮递员创建课程时,我遇到了一个问题,它创建成功但是当我从我的浏览器尝试时,它在网络选项卡中给了我 500 个错误。但是在控制台中我得到了 CORS 错误和 500 错误。如果有人遇到此类问题,我将 SS 包括在下面,请帮助我。我正在尝试来自 stackoverflow 的所有类似解决方案。
从来源 'http://localhost:3000' 访问 '' 的 XMLHttpRequest 已被 CORS 策略阻止:否 '请求的资源上存在 Access-Control-Allow-Origin' 标头。
const apiClient = axios.create({
baseURL: "https://my-link",
withCredentials: false,
accesscontrolalloworigin: "*",
accesscontrolallowMethods: "GET, POST, PUT, DELETE, PATCH, OPTIONS",
});
// Create Lesson
export const createLesson = (lessonData, token) => async (dispatch) => {
try {
dispatch({ type: NEW_LESSON_REQUEST });
const config = {
headers: {
Authorization: `Bearer ${token}`,
'Access-Control-Allow-Origin' : '*',
'Access-Control-Allow-Credentials':true,
'Access-Control-Allow-Methods':'GET,PUT,POST,DELETE,PATCH,OPTIONS',
},
};
const { data } = await apiClient.post(`lesson/add`, lessonData, config);
dispatch({
type: NEW_LESSON_SUCCESS,
payload: data,
});
} catch (error) {
dispatch({
type: NEW_LESSON_FAIL,
payload: error.response,
});
}
};
回答如下:
您需要允许来自后端的来源。像这样
Access-Control-Allow-Origin: http://localhost:3000
更多推荐
React js axios 在邮递员中工作时出现 CORS 错误
本文发布于:2024-05-30 14:10:52,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
发布评论