#CORS on mobile
1 messages Β· Page 1 of 1 (latest)
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.
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
Are you building for IOS ?
Have you tried building a new route with this logic and test it out ?
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
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...
Your saying that creating a local route and get that data from the remote url the way i posted wont work in production ?
I am sorry, i am no clear the app is not loading its own assets "resources/js/app.js'" and i dont know why he is trying to load from an a ip addres "http://localhost:5174/resources/js/app.js'"
my probles does not have any involvement with remote requests
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
Give more details step by step, example
step 1, coding
step 2, npm run build
step 3, php artisan native:install
step 4, php artisan native:run (and here is where it crashes, and post a little more of context of the error)
I currently have my app running on a physical device, requesting data from an external source which works. So i don't see why it should not work like that.
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
CORS is related to browser security. It typically applies to requests made from the browser itself to another source besides the origin URL where the app is hosted. If you create a route in the app, itβs not using the browser β itβs the server retrieving data from a remote source.
I also use this approach for verification with laravel sanctum.