【问题标题】:Flutter web api calling XMLHttpRequest errorFlutter web api调用XMLHttpRequest错误
【发布时间】:2020-10-15 18:17:11
【问题描述】:
     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

【问题讨论】:

    标签: flutter flutter-layout flutter-web


    【解决方案1】:

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

    在你的网址前添加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

    【讨论】:

    • 感谢您的回答,当我在 crome 上模拟它时,它适用于我的项目,但是当我在像素手机上模拟它时,它仍然没有给出答案,我必须在仅在 crome 上进行更改之前添加它错误已显示,像素上没有发生任何事情
    • 是否有消除此错误的最佳实践,而不在您的 API 端点前使用此 URL ????
    • 这真的取决于你的要求 - 如果你用数据管理服务器,你可以允许你的来源,如果你使用服务 - 有时他们会有一个部分,你可以允许 IP/URL您可以接受来自的请求
    猜你喜欢
    • 2021-11-07
    • 1970-01-01
    • 2022-01-07
    • 2020-05-28
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    相关资源
    最近更新 更多