Hello Guys How are you all? Hope You all are fine. When I was trying to run my flutter app and suddenly I get the following error in my stack track. flutter Web getting ‘XMLHttpRequest’ error while making HTTP call. So today Here I come with all possible solutions for this error.
We are providing you all possible solutions to solve this error. let’s start this article without wasting your time.
How Flutter Web getting ‘XMLHttpRequest’ error while making HTTP call Error Occurs ?
I am working in flutter web. I am using api to fetch data but I got error from http plugin here is my stack track.
Launching lib\main.dart on Chrome in debug mode...
Syncing files to device Chrome...
Debug service listening on ws://127.0.0.1:56619/FsXy3a4ZrZg=
Debug service listening on ws://127.0.0.1:56619/FsXy3a4ZrZg=
Error: XMLHttpRequest error.
Here is my code
Future getApiData()async {
final response = await http.get(
'http://my_web_url/project_pinn/list_cat.php',
headers: {
"Accept": "application/json",
"Access-Control_Allow_Origin": "*"
});
print(response.statusCode);
print(response.body);
}
How to Solve Flutter Web getting ‘XMLHttpRequest’ error while making HTTP call Error?
- How to Solve Flutter Web getting 'XMLHttpRequest' error while making HTTP call Error?
To Solve Flutter Web getting 'XMLHttpRequest' error while making HTTP call Error Most cases of this error just add
Access-Control-Allow-Origin
value in the header might resolve the issue. Just make sure to replace all Underscore by Dash. - Flutter Web getting 'XMLHttpRequest' error while making HTTP call
To Solve Flutter Web getting 'XMLHttpRequest' error while making HTTP call Error Most cases of this error just add
Access-Control-Allow-Origin
value in the header might resolve the issue. Just make sure to replace all Underscore by Dash.
Solution 1: add Access-Control-Allow-Origin
Most cases of this error just add Access-Control-Allow-Origin
value in header might resolve the issue. Just make sure to replace all Underscore by Dash.
header("Access-Control-Allow-Origin: *");
Solution 2: Fix the typo
- Fix the typo (replace underscores by dashes):
header("Access-Control-Allow-Origin: *");
- Add the following header in your PHP API code:
header("Access-Control-Allow-Headers: Access-Control-Allow-Origin, Accept");
Solution 3: Upgrade plugin version
Just Update your http plugin verion.
Summery
So, It’s All About This Error. I hope this tutorial helps you to Solve your error. Please Comment Below if You stucks anywhere with my code. And please comment below on which solution worked for you. Thank You.
Also Check Out Below Tutorials
- Unhandled Exception: Bad state: Insecure HTTP is not allowed by platform in a flutter
- The argument type ‘String’ can’t be assigned to the parameter type ‘int’
- How to use Conditional statement in widget in flutter?
- How to use Hexadecimal HEX Color Code String in Flutter Dart ?
- Also Read How to Add Borders to a Widget In Flutter ?