[英]Flutter DioError [DioErrorType.response]: XMLHttpRequest error
I have a Localhoset and want to get the data from this local host in Flutter and show it with clicking on the button.我有一个 Localhoset,想从 Flutter 中的本地主机获取数据,并通过单击按钮显示它。 the get request successfully finishes but the nstead of printing the result i recieve the following error!
获取请求成功完成,但我没有打印结果,而是收到以下错误!
> Running with unsound null safety
For more information see https://dart.dev/null-safety/unsound-null-safety
Debug service listening on ws://127.0.0.1:59432/vuDMHfesIRs=/ws
DioError [DioErrorType.response]: XMLHttpRequest error.
dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 909:28 get current
packages/dio/src/dio_mixin.dart 819:20 assureDioError
packages/dio/src/dio_mixin.dart 678:13 _dispatchRequest
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 60:31 <fn>
dart-sdk/lib/async/zone.dart 1618:54 runBinary
dart-sdk/lib/async/future_impl.dart 169:20 handleError
dart-sdk/lib/async/future_impl.dart 719:46 handleError
dart-sdk/lib/async/future_impl.dart 740:13 _propagateToListeners
dart-sdk/lib/async/future_impl.dart 550:5 [_completeError]
dart-sdk/lib/async/future_impl.dart 606:7 callback
dart-sdk/lib/async/schedule_microtask.dart 40:11 _microtaskLoop
dart-sdk/lib/async/schedule_microtask.dart 49:5 _startMicrotaskLoop
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 166:15 <fn>
I test the same code and the same Process with anothe api it was working?我用另一个 api 测试相同的代码和相同的进程,它工作正常吗? what could it be?
会是什么呢?
here is the get method:这是get方法:
Dio dio = new Dio();
void getCountries() async{
try {
var response = await dio.get('http://b21dfa1ea157.ngrok.io/gettest'
);
dio.options.headers['content-Type'] = 'text/plain; charset=UTF-8';
dio.options.headers['Access-Control-Allow-Origin'] = '*';
dio.options.headers['Access-Control-Allow-Methods'] = 'GET , POST';
print(response.data);
} catch (e) {
print(e);
}
}
and also through clicking the Button it calls the method:并且还通过单击 Button 它调用方法:
RaisedButton(onPressed: getMethod)
Could anybody help me?有人可以帮助我吗?
This seems to be a CORS (cross-origin resource sharing) issue.这似乎是 CORS(跨域资源共享)问题。 Common workarounds made here for development builds is by disabling security using
--disable-web-security
argument when the app is run.此处为开发构建制定的常见解决方法是在应用程序运行时使用
--disable-web-security
参数禁用安全性。
For production builds, you need to enable the CORS request on your server-side to fix this.对于生产版本,您需要在服务器端启用 CORS 请求来解决此问题。 Depending on your setup, you won't be needing to make changes on the client app.
根据您的设置,您无需在客户端应用程序上进行更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.