F
F
faleaksey2019-05-10 17:26:20
React
faleaksey, 2019-05-10 17:26:20

Basic auth in React?

Good day to all! in the component, in the componentDidMount method, I check the user authentication in the following way:

axios({
  url: '/remap/1.1/entity/organization',
  method: 'post', // default
  baseURL: 'https://domain-test/api/',
  // headers: {'X-Requested-With': 'XMLHttpRequest'},
  auth: {
    username: 'admin',
    password: '123456'
  },
  responseType: 'json',
  responseEncoding: 'utf8',
}).then(function (response) {
  console.log(response);
})
.catch(function (error) {
  console.log(error);
});

in and toge error: 5cd589ad4ae70447660613.png
PS uncommented // headers: {'X-Requested-With': 'XMLHttpRequest'} , does not help (and should not)
How to fix it? the request is sent locally, and from the server the same error

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
camoo, 2019-05-10
@faleaksey

You need to enable CORS for your local domain on your web server.
Express.js middleware example:

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

Source

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question