React js axios 在邮递员中工作时出现 CORS 错误

编程入门 行业动态 更新时间:2024-10-05 01:19:01

React js axios 在<a href=https://www.elefans.com/category/jswz/34/1770658.html style=邮递员中工作时出现 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小时内删除。
本文标签:邮递员   错误   工作   js   React

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!