[英]CORS ERROR in React js axios when its working in postman
In my Mern Stack Project I am facing a problem when I am creating a Lesson from postman its created successfully but when I am trying from my browser its given me 500 error in network tab.在我的 Mern Stack 项目中,当我从邮递员创建课程时,我遇到了一个问题,它已成功创建,但是当我从浏览器尝试时,它在网络选项卡中给了我 500 错误。 But in console i got CORS error and also 500 error.
但是在控制台中我得到了 CORS 错误和 500 错误。 I am including SS in bellow if anyone can face this kind of problem please help me.
如果有人遇到这种问题,我将SS包括在下面,请帮助我。 I am trying all the similiar solution from stackoverflow.
我正在尝试来自 stackoverflow 的所有类似解决方案。
Access to XMLHttpRequest at 'https://lms-api-v1.coderslab.com.bd/api/v1/lesson/add' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.从源“http://localhost:3000”访问“https://lms-api-v1.coderslab.com.bd/api/v1/lesson/add”的 XMLHttpRequest 已被 CORS 策略阻止:无“访问” -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, }); } };
you need to allow origin from backend.您需要允许来自后端的来源。 like this
像这样
Access-Control-Allow-Origin: http://localhost:3000
在后端添加这个
Access-Control-Allow-Origin: '*'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.