我的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