flutter dart /扑动:Http请求引发XMLHttpRequest错误

ssgvzors  于 2022-12-05  发布在  Flutter
关注(0)|答案(5)|浏览(274)

我试图使一个RSS阅读器与Flutter,但程序提出了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刮刀输出:

[√] 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
2nc8po8w

2nc8po8w1#

1-转到flutter\bin\cache并删除一个名为:flutter_工具.图章
2-转到flutter\packages\flutter_tools\lib\src\web并打开文件chrome. dart。
3-查找“--disable-extensions”删除并添加4.步骤
4-添加“--禁用Web安全性”

cvxl0en2

cvxl0en22#

多亏了@Reza M,我才能发现问题。我在Web配置中缺少'--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'
关于扑动的说明来自this立柱。

b4lqfgs4

b4lqfgs43#

出现此错误的原因是,当您使用Uri.parse(url)时,应该使用Uri.https('urlPath','callPath')(例如:https('api.to. consults','login')),我花了几天时间寻找一个解决方案,但没有任何效果,直到我改变了这个。我希望它能帮助一些人。示例

...
var uri = Uri.https('my.apiurl.com', 'my/api/endopoint/path');
var response = await client.get(uri);
...

您可能会注意到,“my.apiurl.com”不包含“http://”或“https://”,以防它携带您应相应地称为Uri.http或Uri.https的协议。
还要注意,“my/api/endopoint/path”是要查询的端点的路径,例如“api/users/login”或“api/categories '。

wj8zmpe1

wj8zmpe14#

我尝试了很多解决方案,但一个工作,我们只是在您的请求中使用以下标题:

headers: {
      "Access-Control-Allow-Origin": "*",
      'Content-Type': 'application/json',
      'Accept': '*/*'
    }

并确保在请求中使用https

ds97pgxw

ds97pgxw5#

在我的情况下,我使用我的手机热点互联网,我只是改变了我的互联网到其他运营商和它的工作。原因我发现这个解决方案,因为我的浏览器是导航到“http//one.airtel.in/rcconf”每一次。

相关问题