flutter - Flutter Web App:XMLHttpRequest 错误 - 响应标头 _map不支持的操作:未知库?
问题描述
从这里的许多帖子来看,很多人都遇到了类似的问题。
我试图理解这里解释的 CORS 协议https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS和这里 https://auth0.com/blog/cors-tutorial-a-guide -to-cross-origin-resource-sharing/
在我的 Flutter Web 应用程序中,我进行了以下调用:-
Future<http.Response> fetchWebData() async {
return (http.get(Uri.parse('http://xxx.xxx.xxx.xxx:xxxxx/?email=a@b.com')));
}
服务器写的是Java接收到以下记录:-
GET /?email=a@b.com HTTP/1.1
Host: xxx.xxx.xxx.xxx:xxxxx
Connection: keep-alive
User-Agent: Mozil
la/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36
Accept: */*
Origin: http://localhost:58559
Referer: http://localhost:58559/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
从包含“起源”头部我得出结论这是一个简单的 CORS 请求并回复:-
HTTP/1.1 200 OK
Date: Sun, 23 May 2021 13:51:56 GMT
Server: MRS_Server
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Content-type: text/plain;charset=utf-8
Content-Length: 90
Access-Control-Allow-Methods: GET
Keep-Alive: timeout=2, max=100
Connection: Keep-Alive
{"book": [{"id": "01","language": "Java","edition": "third","author": "Herbert Schildt"}]}
为了理解问题,我在
unawaited(xhr.onError.first.then((_) { <<<BREAKPOINT
// Unfortunately, the underlying XMLHttpRequest API doesn't expose any
// specific information about the error itself.
completer.completeError(
ClientException('XMLHttpRequest error.', request.url),
StackTrace.current);
}));
并查看了 request.headers._map ,其中显示:-
<getObject>Unsupported operation :unknown library
一些参考资料说 Content-type: 应该是“文本/纯文本”,而其他节目我希望有人能解释我做错了什么?什么是未知图书馆?
史蒂夫
解决方案
我在尝试向基于 Java 的简单 HTTP 服务器发出 GET / POST 请求时遇到了这个问题。
花了几天后,我发现问题确实与 CORS 相关,而不是 Flutter 相关。
对我来说,解决方案是在服务器端包含一个标题“Access-Control-Allow-Origin:*”:
httpExchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
这终于奏效了——现在 GET 和 POST 请求运行良好。
Flutter 有一点:恕我直言,团队可以更好地记录这个异常。
推荐阅读
- arrays - Vue unshift 只重复最后一项
- sql-server - mvn build failed 给出错误,'sqlcmd' 无法识别。?
- solr - Solr 7.3.0 copyField 无法按预期工作
- python - Django admin.site.register 为模型类抛出 TypeError
- javascript - Node.js foreach + 承诺
- email - SPF 不工作
- string - Golang 不会转义字符串变量
- c++ - 无法打开 *.lib Visual Studio 2017
- python - 在 EB .ebextensions 中运行 celery worker -A app 时没有名为 app 的模块
- firebase - 创建新记录始终为空 firebase