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: "batman@dc.com", password: "111"}

   fetch('http://localhost:8080/user/login', {
        method: 'post',
        body : JSON.stringify(data),
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        dataType : 'json'
    })
Anoop LL
  • 1,548
  • 2
  • 21
  • 32
Dixy Xavier
  • 667
  • 2
  • 6
  • 20

2 Answers2

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.
nehabo
  • 61
  • 1
  • 3
  • Similar problem, got resolved by point 2 i.e. appending '/'. Can you elaborate more as why this is happening? – ajaykumar Oct 05 '17 at 06:59
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.

Community
  • 1
  • 1
Andrea Carraro
  • 9,731
  • 5
  • 33
  • 57