gpt4 book ai didi

http - 如何在 Flutter For Web 上发起 HTTP 请求?

转载 作者:IT王子 更新时间:2023-10-29 06:52:29 24 4
gpt4 key购买 nike

我正在构建一个向 NodeJS 服务器执行 HTTP 请求的应用程序,但是当我执行 HTTP 请求时,结果如下:

Access to XMLHttpRequest at 'http://127.0.0.1:8000/' from origin 'http://127.0.0.1:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Uncaught (in promise) Error: XMLHttpRequest error.
dart:sdk_internal 41864:30 get current
package:http/src/packages/http/src/browser_client.dart 84:22 <fn>
dart:sdk_internal 99587:96 <fn>


at Object.dart.createErrorWithStack (dart_sdk.js:4617)
at Object.async._rethrow (dart_sdk.js:28723)
at async._AsyncCallbackEntry.new.callback (dart_sdk.js:28719)
at Object.async._microtaskLoop (dart_sdk.js:25352)
at async._startMicrotaskLoop (dart_sdk.js:25358)
at dart_sdk.js:25433

这是我使用 'package:http/http.dart' 作为 http; 发出 HTTP 请求的代码:

void requestGet(String endpoint, Callback cb) async {
return await http.get(Uri.encodeFull(url + endpoint),
headers: {"Accept": "application/json"}).then((http.Response response) {
print(response.body);
});
}

void requestPost(String endpoint, Map data, Callback cb) async {
return await http.post(Uri.encodeFull(url + endpoint),
body: data,
headers: {"Accept": "application/json"}).then((http.Response response) {
print(response.body);
});
}

最佳答案

通过在我的 NodeJS 服务器中启用 CORS 解决了有关 HTTP 请求的问题 here是文档。

关于http - 如何在 Flutter For Web 上发起 HTTP 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57340929/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com