Skip to content
Advertisement

CORS Errors only with 400 bad request react fetch request

I’m trying to make 'POST' request in react but i’m getting a few problems regarding CORS. I was just following what console says and fix them in server side [which is PHP] and client side everything works fine when the status is 200 but when status 400 it shows

login:1 Failed to load http://192.168.0.102/API/: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://localhost:3000‘ is therefore not allowed access. The response had HTTP status code 400. If an opaque response serves your needs, set the request’s mode to ‘no-cors’ to fetch the resource with CORS disabled.

I’ve tried to add mode: 'no-cors' but that’s doesn’t work it shows

Uncaught (in promise) SyntaxError: Unexpected end of input

Server Side ‘PHP Slimframework’ headers:

JavaScript

Client Side

src/actions/userActions.js

JavaScript

src/components/layout.js

JavaScript

here screenshot getting this error only with bad request like 400 enter image description here

Please let me know if I miss out any information.

If this has already been asked, I would greatly appreciate if you are able to point me in the right direction.

Thank you so much!

Advertisement

Answer

The problem is with your “php exceptions” on the server-side, When you are throwing an error the headers are not set.

You have to set a few header when you throw the exceptions:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement