简体   繁体   English

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

[英]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.

相关问题 与React,Axios,Express一起使用时如何解决CORS错误? - How to solve CORS error when using with React, Axios, Express? Vue JS 中 axios 的 Slack API CORS 错误 - Slack API CORS error with axios in Vue JS Url 得到 axios 在反应和 postman 中工作,但在节点 Js 中给出 403 错误 - Url get axios works in react and postman but gives 403 error in node Js 错误:无法在将标头发送到客户端后设置标头 - 当我使用 axios 与邮递员进行反应时 - Error: Cannot set headers after they are sent to the client - when i use axios in react not with postman POST 请求正在使用 POSTMAN,但是当我尝试使用 Axios 从浏览器(UI)调用相同的链接时,它显示 404 错误 | node.js - POST request is working using POSTMAN, but when I'm trying to call the same link using Axios from browser(UI) it's showing 404 error | node.js 启用CORS-在Heroku上部署Node.js + React / Redux + Axios - Enable CORS - Node.js + React/Redux + Axios deployed on Heroku 使用 postman 但不适用于 axios 的护照本地策略,“缺少凭据”错误 - Passport local strategy working with postman, but not with axios, "missing credentials" error react - axios - api 已被 CORS 策略错误阻止 - react - axios - api has been blocked by CORS policy error Axios 网络错误取决于 Node JS 中的 CORS 策略 - Axios Network Error depending on CORS policy in Node JS Axios 发布请求在 React JS 中不起作用 - Axios Post Request is Not Working in React JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM