skip to Main Content

i am trying to creat log in app using Django and react .

my js :

const client = axios.create({
baseURL: "http://127.0.0.1:8000"});

//my submitRegistration function

function submitRegistration(e) {
e.preventDefault();
client.post(         //every time I got cors error here//      
  "/api/register",
  {
    email: email,
    username: username,
    password: password
  }
).then(function(res) {
  client.post(
    "/api/login",
    {
      email: email,
      password: password
    }
  ).then(function(res) {
    setCurrentUser(true);
  });
});}

django cors setup:

CORS_ALLOWED_ORIGINS = [
'http://localhost',
'http://127.0.0.1',
'http://0.0.0.0',      ]  CORS_ALLOW_CREDENTIALS = True

I dont know why I got cors error here.any suggestion please!

2

Answers


  1. django-cors-headers

    See the example there. You forgot the port (this should be a port of your react application)

    CORS_ALLOWED_ORIGINS = [
    "http://127.0.0.1:8000",
    ]
    
    Login or Signup to reply.
  2. The CORS (Cross-Origin Resource Sharing) error typically occurs when making requests from a web application hosted on one domain to a server on another domain. In your case, it seems you are running your Django server on http://127.0.0.1:8000, and your React app is hosted on a different localhost port http://localhost:<your-react-port>.

    CORS_ALLOWED_ORIGINS = [
        'http://localhost:<your-react-port>',
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search