E
E
Egor Zuzlov2019-09-09 18:08:30
Node.js
Egor Zuzlov, 2019-09-09 18:08:30

"Redirect is not allowed for a preflight request" - how to solve it?

Hello everyone, I ran into such a problem:
5d766ce149500374687369.png

I read on the Internet that the matter was in the CORS policy and solved it in the following way:

// Исправляем ошибки CORS
app.use((req, res, next) => {
  res.setHeader('Access-Control-Allow-Origin', '*')
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS')
  res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With')

  next()
})

The request is made like this:
fetch('http://*********************/auth/login', {
        method: 'POST',
        body: JSON.stringify({ email: emailLogin.value, pass: passwordLogin.value }),
        headers: { 'Content-Type': 'application/json' }
})


Those. everything happens from localhost:8888/auth to http://*************************/auth/login

But that would be fine, only from the first screen you can see that it also complains about redirect .
My theory is that some headers should be allowed , but I can't figure out which ones.
I ask you to help me with my torment)))

UPD:
Added middleware cors
const express = require('express')
const cors = require('cors')
const app = express()

app.use(cors())

In the end, this did not help either.

If you do this not on localhost, but on hosting, then it gives the following:
5d775cddf0e5e768522867.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2019-09-09
@notiv-nt

if (req.method === 'OPTIONS') {
  res.setHeader('Access-Control-Allow-Origin', '*')
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS')
  res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With')

  res.end();
}

As far as I remember it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question