Flutter web api 调用 XMLHttpRequest 错误

aba*_*air 5 flutter flutter-layout flutter-web

     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);
}
Run Code Online (Sandbox Code Playgroud)

}

请参阅上面的代码 flutter web api 调用错误 xmlhttprequest

错误

代码

Jac*_*Lee 14

看起来 CORS 正在阻止它,请尝试在您的 URL 前面添加一个 CORS 代理。

将此添加到您的 URL https://cors-anywhere.herokuapp.com/前面

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

一旦您确认它正在工作,最好拥有自己的代理,因此使用以下方法创建一个:

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