Fixing Cross-Origin Issues in Flutter Web with Local PHP Servers
When migrating a Flutter mobile app to web, you often encounter a specific friction point: network requests that work seamlessly on an Android emulator or iOS simulator fail immediately upon opening the app in Chrome. The error stack trace usually screams XMLHttpRequest error, leaving developers scratching their heads because the URL looks perfectly valid and the backend code has already been verified on other platforms.
The root cause lies in how browsers handle security policies differently for web versus mobile contexts, specifically regarding the Cross-Origin Resource Sharing (CORS) protocol. When you run a Flutter app on mobile, the request originates from your device's operating system or a native WebView that typically bypasses strict CORS checks for localhost. However, when the same code runs in a browser like Chrome, the browser enforces a strict same-origin policy. If the server does not explicitly grant permission for the origin making the request, the browser blocks the response before your Dart code even sees it, resulting in the generic XMLHttpRequest error you are seeing.
Your current PHP backend is attempting to resolve this by setting headers like Access-Control-Allow-Origin: *. While this header allows any domain to read the response, simply adding it to a local development setup often isn't enough due to how modern browsers interpret credentials. Notice in your Flutter code that you are sending custom headers (Accept and Access-Control_Allow_Origin) alongside the request. This is a common pitfall; you must never send custom headers for cross-origin requests unless the server explicitly permits them.
If you look at your PHP headers, you have included Access-Control-Allow-Credentials: true. When credentials (cookies, authorization headers) or custom request headers are involved, browsers strictly require that the Access-Control-Allow-Origin header contains a specific origin rather than a wildcard (*). If you send both Access-Control-Allow-Origin: * and Access-Control-Allow-Credentials: true, the browser will reject the response. To fix this without changing your PHP logic entirely, you must stop sending custom headers in your Dart client unless they are whitelisted on the server side.
Here is how to adjust your Flutter code to align with standard CORS behavior when running locally:
Future getdata() async {
// Remove custom 'Access-Control_Allow_Origin' header from the request
final response = await http.get(
'http://localhost:80/web_service/calview.php',
headers: {
"Accept": "application/json",
});
print(response.statusCode);
print(response.body);
}
If your PHP server logic remains unchanged and you strictly need to allow all origins, removing the Access-Control-Allow-Credentials header from the PHP response is often the easiest path forward for development. This allows you to use * for the origin without triggering the browser's security block. However, in a production environment, you should replace * with your specific Flutter Web deployment URL (e.g., https://yourapp.laravelcompany.com).
For persistent local development where you want to avoid modifying PHP headers every time you switch between mobile and web, configuring your localhost is the more robust solution. You can modify your system's hosts file or launch Chrome with flags that bypass CORS for specific origins, though this is generally discouraged for production-grade security. Instead, ensure your development server (Apache in your case) is configured to handle these headers correctly before serving the content.
Finally, if you are deploying a Flutter Web app alongside a Laravel backend, the same principles apply but require stricter configuration. You should configure your .env file and your web server (Nginx or Apache) to serve the API calls from localhost explicitly, ensuring that the CORS headers match the exact origin of your deployed web application. This approach ensures that whether you are debugging in Chrome or running on a device, the network layer behaves consistently.
