【发布时间】:2020-12-29 17:54:12
【问题描述】:
我一直在努力解决flutter 的PWA 项目中的cors 问题,我正在关注这个
how to solve flutter CERTIFICATE_VERIFY_FAILED error while performing a POST request?.
我正在使用universal_io 包,因为 dart.io 不能用于 web...这是代码的一部分
HttpClient client = new HttpClient();
client.badCertificateCallback =
((X509Certificate cert, String host, int port) => true);
String url = 'https:xxxx.php';
Map map = {
"a": "a",
"b": "b"
};
HttpClientRequest request = await client.postUrl(Uri.parse(url));
request.headers.set('content-type', 'application/json');
request.add(utf8.encode(json.encode(map)));
if (request is BrowserHttpClientRequest) {
request.credentialsMode = BrowserHttpClientCredentialsMode.include;
}
HttpClientResponse response = await request.close();
print(response.toString());
String reply = await response.transform(utf8.decoder).join();
print(reply);
但我得到这样说的错误
--------------------------------------------------------------------------------
BrowserHttpClient received an error from XMLHttpRequest (which doesn't tell
reason for the error).
HTTP method: POST
URL: https://www.rscm.co.id/apirscm/v2.php
Origin: http://localhost:64121
Cross-origin request!
XmlHttpRequest 'credentials mode' is enabled.
Did the server send the following mandatory headers?
* Access-Control-Allow-Credentials: true
* Access-Control-Allow-Origin: http://localhost:64121
* In credentials mode, '*' would fail!
* Access-Control-Allow-Methods: POST
有没有办法解决这个问题?
【问题讨论】:
-
我认为 Dio 包在网络上解决了这个问题..但仍然不确定
-
嗨,谢谢你的建议,我也会尝试使用 Dio 包
标签: flutter cors x509certificate flutter-web