如何解决DioErrorType.RESPONSE 无效媒体类型
我的 flutter 项目在 android 上运行良好,但是当我尝试将它放到 web 上时我遇到了一些麻烦。我使用 Graphql Faker 作为假后端,它适用于 android 但对于 flutter web 它总是抛出以下错误
╔╣ Request ║ POST
║ http://localhost:9002/graphql
╚══════════════════════════════════════════════════════════════════════════════════════════
╔╣ DioError ║ Status: 500 Internal Server Error
║ http://localhost:9002/graphql
╚══════════════════════════════════════════════════════════════════════════════════════════
╔ DioErrorType.RESPONSE
║ {
║ errors: [{message: invalid media type}]
║ }
╚══════════════════════════════════════════════════════════════════════════════════════════
下面是客户端的样子
GraphQLClient graphQLClient() {
final dio = Dio(
BaseOptions(
connectTimeout: 3 * 1000,contentType: 'application/json',),);
final host = () {
try {
if (Platform.isAndroid) return MY_IP;
} catch (_) {}
return 'localhost';
}();
final graphqlEndpoint = 'http://$host:9002/graphql';
final WebSocketLink _wsLink = WebSocketLink('ws://$host:4000/graphql');
final Link _dioLink = DioLink(
graphqlEndpoint,client: dio,);
// split request based on type
final _link = Link.split(
(request) => request.isSubscription,_wsLink,_dioLink,);
return GraphQLClient(
cache: GraphQLCache(),link: _link,);
}
任何解决此问题的评论/答案将不胜感激
解决方法
这看起来像是 CORS 错误,graphql-faker 文档中有一节是关于 cors 的:
--cors-origin CORS: Specify the custom origin for the Access-Control-Allow-Origin header,by default it is the same as Origin header from the request
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。