简体   繁体   中英

Access-Control-Allow-Origin’ missing - reactjs

I'm building a signup page with react, I am trying to make an HTTP request with axios.get when my component mounts but I'm getting the No 'Access-Control-Allow-Origin:

在此处输入图像描述

Here is my server.js code:

// server.js
    const express = require('express');
    const mongoose = require('mongoose');
    const bodyParser = require("body-parser");
    const users = require("./routes/users");
    const profile= require("./routes/profilew");
    const post= require("./routes/post");
    
    const app = express();
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(bodyParser.json());
    // config db
    const db = require('./config/Keys').mongoURI ;
    mongoose
      .connect(db)
      .then(() => console.log("MongoDB Connected"))
      .catch(err => console.log(err));
    
    app.use("/users", users);
    app.use("/profile", profile);
    app.use("/post", post);
    
    
    app.get('/',(req,res)=>{
        res.send('hello')
    })
    
    
    
    app.listen(5000, err => {
        if (err) console.log("connection to server failed");
        console.log("connected on port 5000");
      });

Your server should allow requests from :3000 port. Usually, cross-domain requests disable on most servers (I mean IIS/Apache/Node).

You should set access-control-allow-origin on your server with http://localhost:3000 or * value. How to do it see here .

Read more about cross-domain restrictions in specs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM