#CORS on mobile

1 messages Β· Page 1 of 1 (latest)

minor tendon
#

You shoul add https:127.0.0.1/ to your cors configuration file within allowed_origins... But just while develop or debug, this setup in production could be dangerous

molten linden
#

Is this a fetch request in browser / javascript ? I had the same problem when fetching data from the app server. What i did was create a route inside the app and get the data you would need. Something along these lines

Web.php

Route::get('/json/agents', function () {
  return Helper::agentsJson();
});

**Helper::agentsJson() **

    /**
     * @return array
     * @throws ConnectionException
     */
    public static function agentsJson(): array
    {
        // Get the agents
        return Http::withOptions([
            'verify' => config('apptabai.verify_ssl')
        ])->get(config('apptabai.app_host_url') . '/json/agents/cache/' . config('apptabai.manager_id'))->json();
    }

And forward that to a blade view or a fetch that uses the new route. This way you don't have to deal with CORS since the request comes from the local server instead of the browser.

raw frost
#

I am building my front with vue3

#

with php artisan serve it works fine, but when i try to run on my mobile sometimes dont work

#

and always is the assets no being loaded and a blank screen

molten linden
#

Are you building for IOS ?

raw frost
#

No

#

only android

molten linden
#

Have you tried building a new route with this logic and test it out ?

raw frost
#

this is my only route
// Catch-all route for Vue SPA
Route::get('/{any}', function () {
return view('app');
})->where('any', '.*');

#

if i return a text in that route

#

it works fine

minor tendon
#

Maybe for local developments that could be work, but in production environment it doesn't, because the server is who responds with CORS error policy or in this case have you tested with production servers, this affects manly with externals connections, when request is made from browsers this has no affection, but when a third party system (like apps) are involved here is where CORS errors rises

In most cases I recommend you to use saloon https://docs.saloon.dev/ to make servers requests...

Build beautiful API integrations and SDKs with Saloon

molten linden
raw frost
#

my probles does not have any involvement with remote requests

minor tendon
# molten linden Your saying that creating a local route and get that data from the remote url th...

That's right, even when your route is locally, in another class or process you gonna need to make a request server (thiking the server is up already in cloud, custom vpn, laravel cloud services, aws, linode whatever you want), and here is where the server validate if the origin (app) is allowed to communicate whit it, and if is not allowed it should return CORS policy error, but there is a solution you may add the origins allowed to communicate and that's it, the error occurs when trying to make requests on production servers or event, stage, beta servers that are online not physically on your desk hehehehe in the best case nativephp app is allowed by default by default config

minor tendon
molten linden
minor tendon
#

Ohh something weird... I would love to see your setups, maybe here is where your app falls in default setups I don't know

CORS have been a headache and offcialy I thinks cors config is the best way to handle it

molten linden
#

I also use this approach for verification with laravel sanctum.