简体   繁体   English

ReactJS 节点和 Axios:请求的资源上不存在“Access-Control-Allow-Origin”标头

[英]ReactJS Node and Axios :No 'Access-Control-Allow-Origin' header is present on the requested resource

I'm learning programming now so forgive me for any mistakes, I'll be grateful for any tips.我现在正在学习编程,所以请原谅我的任何错误,我将不胜感激任何提示。 I have an API that is hosted in the following domain ("api-myapp.com") and I'm trying from my localhost where I'm creating my front-end to post a form (which only logged in users can send) using axios , but the request takes a long time to complete and when it completes it returns the following error (No 'Access-Control-Allow-Origin' header is present on the requested resource.) (net::ERR_FAILED 504), I've tried some solutions I found on the internet but none seem to have worked, this is my code:我有一个托管在以下域(“api-myapp.com”)中的 API,我正在尝试从我的本地主机创建我的前端以发布一个表单(只有登录的用户可以发送)使用 axios ,但请求需要很长时间才能完成,完成后返回以下错误(请求的资源上不存在“Access-Control-Allow-Origin”标头。)(net::ERR_FAILED 504),我已经尝试了一些我在互联网上找到的解决方案,但似乎都没有奏效,这是我的代码:

FrontEnd:前端:

 try {
        const response = await axios.post('/alunos/', {
            nome,sobrenome,idade,sangue,varinha,patrono,house,sala,
        });
        toast.success('Cadastrado com sucesso');
        console.log(response);
    } catch (e) {
        console.log(e);
        const errors = get(e, 'response.data.errors', []);
        errors.map((error) => toast.error(error));
    }

When you logged in当您登录时

 try {
    const response = yield call(axios.post, '/tokens', payload);
    yield put(actions.loginSuccess({ ...response.data }));
    axios.defaults.headers.Authorization = `Bearer ${response.data.token}`;
    toast.success('Login realizado com sucesso!');
    payload.navigate('/dashboard');
}

BackEnd后端

class App {
  constructor() {
    this.app = express();
    this.middlewares();
    this.routes();
  }

  middlewares() {
    this.app.use(cors({ origin: '*' }));
    this.app.use(helmet({ crossOriginResourcePolicy: false }));
    this.app.use(express.urlencoded({ extended: true }));
    this.app.use(express.json());
    this.app.use('/images/', express.static(resolve(__dirname, '..', 'uploads', 'images')));
  }

  routes() {
    this.app.use('/', homeRoutes);
    this.app.use('/prof', profRoutes);
    this.app.use('/tokens', tokenRoutes);
    this.app.use('/alunos', alunoRoutes);
    this.app.use('/casas', casaRoutes);
    this.app.use('/provas', provaRoutes);
    this.app.use('/materias', materiaRoutes);
    this.app.use('/salas', salaRoutes);
    this.app.use('/fotosAlunos', fotoAlunoRoutes);
    this.app.use('/fotosProf', fotoProfRoutes);
  }
}

You have to enable CORS in backend to allow request through different ports.您必须在后端启用 CORS 以允许通过不同端口的请求。 For your case since you are using express and cors library, you can try this.对于您的情况,因为您使用的是 express 和 cors 库,所以您可以试试这个。

 app.use( cors({ credentials: true, methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS', allowedHeaders: ['Content-Type', 'Authorization'], origin: ['http://localhost:3000', 'http://localhost:3030'], // whatever ports you used in frontend }) );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Reactjs:请求的资源上不存在“Access-Control-Allow-Origin”header - Reactjs: No 'Access-Control-Allow-Origin' header is present on the requested resource 在请求的资源错误上,Axios命中没有“ Access-Control-Allow-Origin”标头 - Axios hitting No 'Access-Control-Allow-Origin' header is present on the requested resource error 由于请求的资源上不存在“Access-Control-Allow-Origin”标头,因此无法请求与 Axios 的链接 - Can't get request a link with Axios due to No 'Access-Control-Allow-Origin' header is present on the requested resource Javascript-所请求的资源上没有“ Access-Control-Allow-Origin”标头 - Javascript - No 'Access-Control-Allow-Origin' header is present on the requested resource 请求的资源上不存在“Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present on the requested resource 角度4-所请求的资源上没有“ Access-Control-Allow-Origin”标头 - Angular 4 - No 'Access-Control-Allow-Origin' header is present on the requested resource NodeJ在所请求的资源上不存在“ Access-Control-Allow-Origin”标头 - NodeJs No 'Access-Control-Allow-Origin' header is present on the requested resource Access-Control-Allow-Origin header 存在于请求的资源上 - Access-Control-Allow-Origin header is present on the requested resource 请求的资源 .htaccess 上不存在 Access-Control-Allow-Origin 标头 - No Access-Control-Allow-Origin header is present on the requested resource .htaccess XMLHttpRequest请求的资源上没有“Access-Control-Allow-Origin”标头 - XMLHttpRequest No 'Access-Control-Allow-Origin' header is present on the requested resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM