Fetch API cannot load [URL]. Response for preflight has invalid HTTP status code 404

Asked

Viewed 627 times

2

Greetings !

I am facing the following problem, I make a request [POST] via Fetch API and "call" does not conclude.

REQUEST VIA Fetch API

const requestInfo = {
    method: 'POST',
    body: JSON.stringify({ email:'[email protected]' , password:'123456', entity:'provider'}),
    headers: new Headers({'Content-type': 'application/json;charset=UTF-8'}),
};
fetch('http://localhost:4212/login',requestInfo)
    .then(response => {
        console.log(response);
        return response.json();
    })
    .then(sucess => console.log(sucess))
    .catch(error => console.log(error));
    }

RESPONSE VIA Fetch API

inserir a descrição da imagem aqui The strange thing is that requisition type [GET] works perfectly.

REQUEST VIA POSTMAN

Via Postman also works [GET/POST]. inserir a descrição da imagem aqui

HEADERS DELIVERED

access-control-allow-credentials →true
access-control-allow-headers →Origin, X-Requested-With, Content-Type, Accept, Accept-Encoding, Content-Encoding, X-Auth-Token
access-control-allow-methods →POST, GET, PUT, OPTIONS, DELETE, PATCH
access-control-allow-origin →*
access-control-max-age →3600
cache-control →no-cache, no-store, max-age=0, must-revalidate
content-encoding →gzip
content-type →application/json;charset=UTF-8
date →Thu, 27 Jul 2017 02:14:50 GMT
expires →0
pragma →no-cache
transfer-encoding →chunked
vary →Accept-Encoding
x-content-type-options →nosniff
x-frame-options →DENY
x-xss-protection →1; mode=block

Does anyone know what it can be ?

Thank you!

  • you use some framework to use url without extension (http://localhost:4212/login)?

2 answers

2

Often, Postman will work because the rules of "Cross Origin Requests" do not apply. If you are using Express.js, you can use this:

https://www.npmjs.com/package/cors

The problem is that your server does not accept requests from a different source. Use Cors to allow your client to access your server.

  • 1

    Thank you for your attention.

1


Thank you for your attention

I solved the problem added that line in Cors settings.

.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()

If I declare only POST/PUT does not work. I have to esudar more to discover. vlw

Browser other questions tagged

You are not signed in. Login or sign up in order to post.