NelmioCorsBundle icon indicating copy to clipboard operation
NelmioCorsBundle copied to clipboard

net::ERR_ABORTED 500 Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

Open VitaliiIsaenko opened this issue 6 years ago • 1 comments

Hello!
I have the following issue when trying to send get request to the server. My server is running on localhost:8000 and the sending server is localhost:8080 that I run only for this testing purpose. There is the request that I send (in browser console with js):

var xhr = new XMLHttpRequest();

xhr.open("GET", "http://localhost:8000/companies/2/");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("loginToken", "my_login_token");

xhr.send(data);

The error that I get:

OPTIONS http://localhost:8000/companies/2/ net::ERR_ABORTED 500 (Internal Server Error)
Access to XMLHttpRequest at 'http://localhost:8000/companies/2/' 
from origin 'http://localhost:8080' has been blocked by CORS policy: 
Response to preflight request doesn't pass access control check: 
It does not have HTTP ok status.

There is my nelmio_cors configurations:

nelmio_cors:
    defaults:
        allow_credentials: false
        allow_origin: ['*']
        allow_headers: ['*']
        allow_methods: ['POST', 'PUT', 'GET', 'DELETE']
        max_age: 3600
        expose_headers: []
        hosts: []
        origin_regex: false
        forced_allow_origin_value: ~
    paths:
        '^/': ~

And here is the method I test (just in case)

    /**
     * @Route("/companies/{id}/", name="getCompanyById", methods={"GET","HEAD"})
     * @param $id
     * @param Request $request
     * @return null|\Symfony\Component\HttpFoundation\JsonResponse
     */
    public function getCompanyById(Request $request, int $id): JsonResponse
    {
         ...
    }

Maybe this dirty hack will help you understand what we need (in public/index.php)

if($_SERVER['REQUEST_METHOD'] == "OPTIONS"){
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Headers: *");
    die;
}

Could you help me with this, please? I have no idea what else to try, already tried many options. P.S. Had a look at this issue discussion as it seems similar, but didn't find any solution there.

VitaliiIsaenko avatar Feb 19 '19 15:02 VitaliiIsaenko

@Seldaek Bump! I have the same issue... workaround (dirty hack) given by @VitaliiIsaenko works like a charm, but how to handle this in the right way?

kowalk avatar Jan 18 '22 13:01 kowalk

If you can still reproduce this please reopen a new issue with config details and which symfony and bundle version you are using. Then we can maybe figure it out.. But as is it seems like it should work and for me it definitely does so I'm not sure how I can help.

Seldaek avatar Feb 15 '23 21:02 Seldaek

I have the same error are you already know how to solve it ?

EliphazBouye avatar Dec 21 '23 10:12 EliphazBouye