【问题标题】:Dart/Flutter: Http request raises XMLHttpRequest errorDart/Flutter:Http 请求引发 XMLHttpRequest 错误
【发布时间】:2022-07-26 20:14:48
【问题描述】:

我正在尝试用 Flutter 制作一个 RSS 阅读器,但程序引发了 XMLHttpRequest error

在我的测试中,我使用 web (Chrome) 作为我的目标平台。

感谢您的帮助。

  Future<RssFeed> fetchFeed() async {
try {
  final response = await http.get(Uri.parse(url), headers: {"Access-Control-Allow-Origin": "*"});
  return RssFeed.parse(response.body);
} catch (e) {
  print(e);
  return RssFeed(title: "Test");
  }}

  var feed = await fetchFeed();

完整的错误日志:

Error: XMLHttpRequest error.
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-

sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 910:28                get current
packages/http/src/browser_client.dart 69:22                                                                                    <fn>
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/zone.dart 1685:54                                              runUnary
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 159:18                                        handleValue
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 766:44                                        handleValueCallback
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 795:13                                        _propagateToListeners
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 592:7                                         [_complete]
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/stream_pipe.dart 61:11                                         _cancelAndValue
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/stream.dart 1288:7                                             <fn>
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 334:14  _checkAndCall
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 339:39  dcall
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/html/dart2js/html_dart2js.dart 37301:58                              <fn>


    at Object.createErrorWithStack (http://localhost:49656/dart_sdk.js:5080:12)
    at Function._throw (http://localhost:49656/dart_sdk.js:20337:18)
    at Function.throwWithStackTrace (http://localhost:49656/dart_sdk.js:20334:18)
    at async._AsyncCallbackEntry.new.callback (http://localhost:49656/dart_sdk.js:40851:18)
    at Object._microtaskLoop (http://localhost:49656/dart_sdk.js:40708:13)
    at _startMicrotaskLoop (http://localhost:49656/dart_sdk.js:40714:13)
    at http://localhost:49656/dart_sdk.js:36191:9

颤振医生输出:

[√] Flutter (Channel stable, 2.10.1, on Microsoft Windows [Version 10.0.19042.1526], locale de-DE)
[√] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
[√] Chrome - develop for the web
[√] Visual Studio - develop for Windows (Visual Studio Community 2022 17.0.5)
[√] Android Studio (version 2021.1)
[√] VS Code (version 1.63.2)
[√] Connected device (4 available)
[√] HTTP Host Availability

依赖关系:

dependencies:
  flutter:
    sdk: flutter



  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.2
  url_launcher: ^6.0.20
  http: ^0.13.0
  flutter_staggered_grid_view: ^0.6.1
  calendar_view: ^0.0.3
  webfeed: ^0.7.0
  string_similarity: ^2.0.0
  sortedmap: ^0.5.1

【问题讨论】:

  • 您似乎面临着 CORS 政策。您应该在没有 CORS 政策的情况下运行 google。阅读这篇文章stackoverflow.com/questions/3102819/…
  • 将该标头添加到 request 将无济于事。它必须由服务器添加到 response 中。您的服务器是否允许您指定允许的来源?
  • 我遇到了同样的问题。最后我解决了this post的帮助

标签: xml flutter dart http rss


【解决方案1】:

1- 转到flutter\bin\cache 并删除一个名为:flutter_tools.stamp 的文件

2- 转到flutter\packages\flutter_tools\lib\src\web 并打开文件chrome.dart。

3- 查找'--disable-extensions' 删除并添加 4.step

4- 添加'--disable-web-security'

【讨论】:

  • 这是否适用于已发布的版本以及它如何适用于除 chrome 之外的其他浏览器?我是否必须为 Microsoft Edge 更改文件?
【解决方案2】:

感谢@Reza M,我可以确定问题所在。我在网络配置中缺少'--disable-web-security'

1- 转到flutter\bin\cache 并删除一个名为:flutter_tools.stamp的文件

2- 转到flutter\packages\flutter_tools\lib\src\web 并打开文件 chrome.dart。

3- 查找'--disable-extensions'

4- 添加'--disable-web-security'

flutter 的说明来自this 帖子。

【讨论】:

  • 但在发布版本中不起作用
  • 您是否找到了适用于构建/发布版本的好替代方案?
猜你喜欢
  • 2019-12-13
  • 2021-06-03
  • 1970-01-01
  • 2018-08-22
  • 2015-08-30
  • 2017-01-29
  • 2019-06-17
  • 2015-06-25
  • 2019-05-15
相关资源
最近更新 更多