Flutter web api 调用 XMLHttpRequest 错误

标签 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);
}
}
请参阅上面的代码 flutter web api 调用错误 xmlhttprequest
Error
Code

最佳答案

看起来 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");
一旦您确认它正在工作,最好拥有自己的代理,因此使用以下方法创建一个:
https://github.com/Rob--W/cors-anywhere/#documentation

关于Flutter web api 调用 XMLHttpRequest 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62572828/

相关文章:

flutter - iOS 上的向后滑动手势被 WillPopScope 覆盖

ios - 在 iOS 中兑换促销代码以换取非消耗品时是否会有 IAP 回调?

Flutter GetX 绑定(bind)?

flutter - 为容器提供固定的宽度和高度与通过 BoxConstraints 类提供约束

flutter - 在Flutter中读取 'lib/generated_plugin_registrant.dart'时出错

android - 使用 Google Maps Flutter 进行 Android 预启动测试的 StackOverflowError

flutter - 如何简单解决多依赖版本冲突?

class - 此类被标记为 '@immutable',但是其一个或多个实例字段不是最终的:

firebase - Flutter web - 无法加载网络图像

关闭并重新打开选项卡时 Flutter web shared_preferences 不可用