Flutter web api 获取 XMLHttpRequest

Flutter web api getting XMLHttpRequest

我的应用程序在 android 上运行良好,但在网络上,我收到错误:

Error: XMLHttpRequest error. dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart

我正在使用 PHP API 生成 Json 数据,我已经添加了 header:

header("Access-Control_Allow_Origin: *");
header("Content-type:application/json;charset=utf-8"); 
header("Access-Control-Allow-Methods: GET");

错误输出:

Error: XMLHttpRequest error.
dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 908:28                get current
packages/http/src/browser_client.dart 69:22                                       <fn>
dart-sdk/lib/async/zone.dart 1687:54                                              runUnary
dart-sdk/lib/async/future_impl.dart 160:18                                        handleValue
dart-sdk/lib/async/future_impl.dart 767:44                                        handleValueCallback
dart-sdk/lib/async/future_impl.dart 796:13                                        _propagateToListeners
dart-sdk/lib/async/future_impl.dart 593:7                                         [_complete]
dart-sdk/lib/async/stream_pipe.dart 61:11                                         _cancelAndValue
dart-sdk/lib/async/stream.dart 1232:7                                             <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 334:14  _checkAndCall
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 339:39  dcall
dart-sdk/lib/html/dart2js/html_dart2js.dart 37332:58                              <fn>
    at Object.createErrorWithStack (http://localhost:52772/dart_sdk.js:5388:12)
    at Object._rethrow (http://localhost:52772/dart_sdk.js:40987:16)
    at async._AsyncCallbackEntry.new.callback (http://localhost:52772/dart_sdk.js:40981:13)
    at Object._microtaskLoop (http://localhost:52772/dart_sdk.js:40808:13)
    at _startMicrotaskLoop (http://localhost:52772/dart_sdk.js:40814:13)
    at http://localhost:52772/dart_sdk.js:36279:9

它在遵循那些 :

之后起作用

1- 转到 flutter\bin\cache 并删除名为:flutter_tools.stamp

的文件

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

3- 查找“--disable-extensions”

4- 添加“--disable-web-security”

我在网络上部署时遇到了同样的问题,请确保您:

  • 将 headers 添加到 .htaccess 文件。
  • 最好使用 POST 而不是 GET。
  • 在您的 flutter 代码中 API 使用 HTTPS 而不是 HTTP。