Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter web api calling XMLHttpRequest error

     Future<void> _getShopListAllJson() async {
try {
  final response = await http.get("http://abair.gq/db_dept_info_all.php");
  if (response.statusCode == 200) {
    print(response.statusCode);
    print(response.body);
     setState(() {
      _data = jsonDecode(response.body) as List;
    });
  } else {
    print("Some error: ${response.statusCode}");
  }
} catch (e) {
  print(e);
}

}

Please see above code flutter web api calling error xmlhttprequest

Error

Code

like image 242
abair Avatar asked Jun 25 '20 10:06

abair


People also ask

What is an XMLHttpRequest error?

How do I fix the XHR error? Technically, this is not error, it is normal response. The message tells you that you just tried to enter a secure room without the key, your error is that you don't have the key/authorization to enter that room. The only solution is to get the key/credentials and include on the request.


1 Answers

Looks like CORS is blocking it, try adding a CORS proxy in front of your URL.

Add this in front of your URL https://cors-anywhere.herokuapp.com/

final response = await http.get("https://cors-anywhere.herokuapp.com/http://abair.gq/db_dept_info_all.php");

Once you can confirm it is working it is best practice to have your own proxy so create one using this method:

https://github.com/Rob--W/cors-anywhere/#documentation

like image 170
Jackson Lee Avatar answered Sep 20 '22 12:09

Jackson Lee