最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

网站源码admin40浏览0评论

React js axios 在邮递员中工作时出现 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
发布评论

评论列表(0)

  1. 暂无评论