Fetch API cannot load the url. Response for preflight is invalid (redirect)
Asked Answered
B

2

10

I use fetch to call a post service and I get the error Fetch API cannot load http://localhost:8080/user/login. Response for preflight is invalid (redirect)'. Status code: 302 Found. The variable 'data' is a json object {email: "[email protected]", password: "111"}

   fetch('http://localhost:8080/user/login', {
        method: 'post',
        body : JSON.stringify(data),
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        dataType : 'json'
    })
Billiot answered 19/2, 2016 at 6:55 Comment(0)
M
6

There maybe several possible reasons you are getting this error.

  • As @toomuchdesign mentioned a possible reason why is because you might need to make a request through https instead of http.
  • Try appending a / to the url. 'http://localhost:8080/user/login/' instead of 'http://localhost:8080/user/login'. As it could be redirecting from a preflight/OPTIONS request.
Microfarad answered 26/5, 2017 at 6:45 Comment(1)
Similar problem, got resolved by point 2 i.e. appending '/'. Can you elaborate more as why this is happening?Fledgy
M
3

The error means that the url you're hitting is responding with a redirect to another url.

In my case I was requesting a resource trough http instead of https, so the server was responding with a redirect to https protocol.

More hints here.

Measures answered 22/12, 2016 at 9:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.