Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Axios in React return error CORS with basic auth #103

Open
Ramil001 opened this issue Feb 3, 2023 · 2 comments
Open

Axios in React return error CORS with basic auth #103

Ramil001 opened this issue Feb 3, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@Ramil001
Copy link

Ramil001 commented Feb 3, 2023

I create react app and i have get data from webservices prestashop 1.7

import axios from 'axios';
import logo from './logo.svg';
import './App.css';


function App() {


  const options = {
    method: 'GET',
    url: 'https://woxxxed.itxcxxy.agency/api/products/6',
    params: {output_format: 'JSON'},
    headers: {Authorization: 'Basic XXXXXXXXXXXXXXNVNzlXSlNYNlU4OVRNWVo6'}
  };
  
  axios.request(options).then(function (response) {
    console.log(response.data);
  }).catch(function (error) {
    console.error(error);
  });

  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
    </div>
  );
}

export default App;

image

I have result error CORS

In postman all is good
image

rapid good
image

Why this axios not working in react? Why my code is bad?

@Ramil001 Ramil001 added the bug Something isn't working label Feb 3, 2023
@codemaster138
Copy link
Contributor

Hi @Ramil001, thanks for reporting your issue.

The error you're seeing is because of a CORS (cross-origin resource sharing) restriction. Browsers supporting CORS (all popular browsers) will block requests if the server doesn't return an appropriate Access-Control-Allow-Origin header.

This is an issue with the way your API is set up; axios is working fine.

Another tip: The way your code is currently set up, the request would be made every single time App is rendered. Instead, your axios call should be inside a useEffect hook.

Hope I could help!

@JPLT123
Copy link

JPLT123 commented Oct 21, 2024

I'm this problem too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants