6
     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

1 Answer 1

17

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thx for your answe, it works on my project when i emulate it on crome but when i emulate it on an pixel phone it still gives no answer, i have to add that before the change only on crome the error was shown and on the pixel nothing hapend
is there is best practice to remove this error, without using this URL in front of your API endpoint ????
It really depends what your requesting - if you manage the server with the data, you can allow your origin, if your using a service - sometimes they will have a section where you can allow an IP/URL that you can accept request from

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.