Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

287
Visualizações
origin http://localhost:3000 has been blocked by CORS policy: The Access-Control-Allow-Origin In my react App

I am beginner of react program. I am just building one small web app using Laravel and React. I suddenly got error I don't know what I did wrong. If anyone help me I will be glade to you. Bellow the error message:

Access to XMLHttpRequest at http://127.0.0.1:8000/sanctum/csrf-cookie from origin http://localhost:3000 has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value http://localhost:4200 that is not equal to the supplied origin.

I am trying like this way:

import React from 'react';
import { BrowserRouter as Router, Route, Switch} from 'react-router-dom';

import MasterLayout from './layouts/admin/MasterLayout';

import Home from './components/frontend/Home';
import Login from './components/frontend/auth/Login';
import Register from './components/frontend/auth/Register';
import axios from 'axios';

axios.defaults.baseURL = "http://127.0.0.1:8000";
axios.defaults.headers.post['Content-Type'] = 'application/json';
axios.defaults.headers.post['Accept'] = 'application/json';
axios.defaults.withCredentials = true;
    
function App() {
  return (
    <div className="App">
        <Router>
          <Switch>
            {/* Homepage */}
            <Route exact path="/" component={Home}/>

            {/* Auth Pages */}
            <Route path="/login" component={Login}/>
            <Route path="/register" component={Register}/>


            <Route path="/admin" name="Admin" render={(props) => <MasterLayout {...props} />} />

          </Switch>
        </Router>
    </div>
  );
}

export default App;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

CORS is a browser mechanism that asks webserver if it is willing to accept request from specific origin. Origin is your hostname + port, meaning localhost:3000, localhost:4200 and localhost:8000 are all different origins.

You can either configure header Access-Control-Allow-Origin on your backend side to accept requests from 'localhost:3000', or you can start your react application on port :4200, since it is already accepted by your backend, or you can use proxy that will make requests to backend from server side.

In production environment you usually have backend and frontend working on same origin, so proxy is a good way to solve CORS problems during development. You also can setup some reverse proxy, like nginx, so your backend and frontend would both be on same origin.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda