我一直在努力解决flutter
的PWA
项目中的cors
问题,我正在遵循这个
如何在执行POST请求时解决flutter CERTIFICATE_VERIFY_FAILED错误?。
我正在使用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
有办法解决这个问题吗?
您可以在chrome的调试模式下使用本地主机服务器,方法如下:在终端打开这个。
open -n -a /Applications/Google Chrome.app/Contents/MacOS/Google Chrome --args --user-data-dir="/Users/i0868/Desktop/Chrome" --disable-web-security
您可以执行类似的操作来启用凭据模式:
final httpClientRequest = MyHttpClientRequest;
if (httpClientRequest is BrowserHttpClientRequest) {
httpClientRequest.credentialsMode = BrowserHttpClientCredentialsMode.include;
}
但更喜欢使用这个库universal.io而不是dart.io