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

Dixy Xavier picture Dixy Xavier · Feb 19, 2016 · Viewed 14.3k times · Source

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'
    })

Answer

nehabo picture nehabo · May 26, 2017

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.