Tuesday, January 11, 2022

[FIXED] API call to my Symfony 5 app with debug mode disabled returns CORS error

Issue

I have this problem with my Symfony 5 app which serves as my API. I am making requests to my Symfony app from another app on the same domain and different subdomain.

Everything is fine when debug mode is set to true APP_DEBUG = 1 in my Symfony app. However if I disable debug mode APP_DEBUG = 0, then my requests from the other app cause CORS errors:

Access to XMLHttpRequest at 'https://api.mamacita365.com/api/getmeals' 
from origin 'https://app.mamacita365.com' has been blocked by CORS policy: 
No 'Access-Control-Allow-Origin' header is present on the requested resource.

I am using nelmio/cors-bundle - here's nelmio_cors.yaml:

nelmio_cors:
    defaults:
        origin_regex: true
        allow_credentials: true
        allow_origin: ['https://api.mamacita365.com', 'https://app.mamacita365.com']
        allow_methods: ['GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE']
        allow_headers: ['Content-Type', 'Authorization']
        expose_headers: ['Link']
        max_age: 3600
    paths:
        '^/': null

Here's .env.local.php:

<?php

// This file was generated by running "composer dump-env prod"

return array (
  'APP_ENV' => 'prod',
  'APP_DEBUG' => 0, // if equals 0 then CORS error, if equals 1 then OK
  // ...
);

Solution

Clear the cache manually (/var/cache/prod folder) or via command php bin/console cache:clear --env=prod



Answered By - DVN-Anakin

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.