无法从 Flutter 调用 Odoo Api
Can't Call Odoo Api From Flutter
我使用 Controller 在 Odoo 中创建了一个 API。身份验证类型是 public,因此无需身份验证即可访问此 API。当我在 Postman 或任何浏览器中调用此 API 时,我没有问题。但是当我在 Flutter 中访问这个 API 时,我得到了一个错误。
这是 Flutter 代码
var response = await Dio().get('https://my-odoo-domain.com/api/sales');
print(response);
这是错误信息
Error: DioError [DioErrorType.response]: XMLHttpRequest error.
Source stack:
dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 910:28 get current
packages/dio/src/dio_mixin.dart 473:35 fetch
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54 runBody
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 123:5 _async
packages/dio/src/dio_mixin.dart 472:31 fetch
packages/dio/src/dio_mixin.dart 468:12 request
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54 runBody
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 123:5 _async
packages/dio/src/dio_mixin.dart 438:33 request
packages/dio/src/dio_mixin.dart 55:12 get
packages/flutter_baru/screens/home_provider.dart 42:32 fetchRoomMappings
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54 runBody
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 123:5 _async
packages/flutter_baru/screens/home_provider.dart 18:33 fetchRoomMappings
packages/flutter_baru/screens/home_provider.dart 14:11 setup
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54 runBody
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 123:5 _async
packages/flutter_baru/screens/home_provider.dart 12:21 setup
packages/flutter_baru/screens/home_screen.dart 13:12 setup
packages/flutter_baru/base/base_stateful.dart 19:14 <fn>
packages/flutter/src/scheduler/binding.dart 1144:15 [_invokeFrameCallback]
packages/flutter/src/scheduler/binding.dart 1089:9 handleDrawFrame
packages/flutter/src/scheduler/binding.dart 862:7 <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/isolate_helper.dart 48:19 internalCallback
at Object.throw_ [as throw] (http://localhost:65437/dart_sdk.js:5067:11)
at http://localhost:65437/packages/dio/src/interceptors/log.dart.lib.js:1004:21
at _RootZone.runBinary (http://localhost:65437/dart_sdk.js:40445:59)
at _FutureListener.catchError.handleError (http://localhost:65437/dart_sdk.js:35374:33)
at handleError (http://localhost:65437/dart_sdk.js:35947:51)
at Function._propagateToListeners (http://localhost:65437/dart_sdk.js:35973:17)
at _Future.new.[_completeError] (http://localhost:65437/dart_sdk.js:35823:23)
at _SyncCompleter.new.[_completeError] (http://localhost:65437/dart_sdk.js:35279:36)
at _SyncCompleter.new.completeError (http://localhost:65437/dart_sdk.js:35203:29)
at onError (http://localhost:65437/dart_sdk.js:35043:49)
at _RootZone.runBinary (http://localhost:65437/dart_sdk.js:40445:59)
at _FutureListener.then.handleError (http://localhost:65437/dart_sdk.js:35374:33)
at handleError (http://localhost:65437/dart_sdk.js:35947:51)
at Function._propagateToListeners (http://localhost:65437/dart_sdk.js:35973:17)
at _Future.new.[_completeError] (http://localhost:65437/dart_sdk.js:35823:23)
at async._AsyncCallbackEntry.new.callback (http://localhost:65437/dart_sdk.js:35859:31)
at Object._microtaskLoop (http://localhost:65437/dart_sdk.js:40708:13)
at _startMicrotaskLoop (http://localhost:65437/dart_sdk.js:40714:13)
at http://localhost:65437/dart_sdk.js:36191:9
如何解决这个问题?
更新
我在 Web 平台上 运行 时出现此错误,但在 Mobile 上 运行 时出现此错误& Desktop 平台我没有错误。我认为问题出在安全性或类似 CORS
问题出在 CORS 上。我需要通过在端点传递 CORS 参数来启用 CORS
@http.route('/my/route', auth='public' ,cors='*')
def my_route(self):
我使用 Controller 在 Odoo 中创建了一个 API。身份验证类型是 public,因此无需身份验证即可访问此 API。当我在 Postman 或任何浏览器中调用此 API 时,我没有问题。但是当我在 Flutter 中访问这个 API 时,我得到了一个错误。
这是 Flutter 代码
var response = await Dio().get('https://my-odoo-domain.com/api/sales');
print(response);
这是错误信息
Error: DioError [DioErrorType.response]: XMLHttpRequest error.
Source stack:
dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 910:28 get current
packages/dio/src/dio_mixin.dart 473:35 fetch
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54 runBody
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 123:5 _async
packages/dio/src/dio_mixin.dart 472:31 fetch
packages/dio/src/dio_mixin.dart 468:12 request
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54 runBody
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 123:5 _async
packages/dio/src/dio_mixin.dart 438:33 request
packages/dio/src/dio_mixin.dart 55:12 get
packages/flutter_baru/screens/home_provider.dart 42:32 fetchRoomMappings
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54 runBody
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 123:5 _async
packages/flutter_baru/screens/home_provider.dart 18:33 fetchRoomMappings
packages/flutter_baru/screens/home_provider.dart 14:11 setup
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54 runBody
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 123:5 _async
packages/flutter_baru/screens/home_provider.dart 12:21 setup
packages/flutter_baru/screens/home_screen.dart 13:12 setup
packages/flutter_baru/base/base_stateful.dart 19:14 <fn>
packages/flutter/src/scheduler/binding.dart 1144:15 [_invokeFrameCallback]
packages/flutter/src/scheduler/binding.dart 1089:9 handleDrawFrame
packages/flutter/src/scheduler/binding.dart 862:7 <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/isolate_helper.dart 48:19 internalCallback
at Object.throw_ [as throw] (http://localhost:65437/dart_sdk.js:5067:11)
at http://localhost:65437/packages/dio/src/interceptors/log.dart.lib.js:1004:21
at _RootZone.runBinary (http://localhost:65437/dart_sdk.js:40445:59)
at _FutureListener.catchError.handleError (http://localhost:65437/dart_sdk.js:35374:33)
at handleError (http://localhost:65437/dart_sdk.js:35947:51)
at Function._propagateToListeners (http://localhost:65437/dart_sdk.js:35973:17)
at _Future.new.[_completeError] (http://localhost:65437/dart_sdk.js:35823:23)
at _SyncCompleter.new.[_completeError] (http://localhost:65437/dart_sdk.js:35279:36)
at _SyncCompleter.new.completeError (http://localhost:65437/dart_sdk.js:35203:29)
at onError (http://localhost:65437/dart_sdk.js:35043:49)
at _RootZone.runBinary (http://localhost:65437/dart_sdk.js:40445:59)
at _FutureListener.then.handleError (http://localhost:65437/dart_sdk.js:35374:33)
at handleError (http://localhost:65437/dart_sdk.js:35947:51)
at Function._propagateToListeners (http://localhost:65437/dart_sdk.js:35973:17)
at _Future.new.[_completeError] (http://localhost:65437/dart_sdk.js:35823:23)
at async._AsyncCallbackEntry.new.callback (http://localhost:65437/dart_sdk.js:35859:31)
at Object._microtaskLoop (http://localhost:65437/dart_sdk.js:40708:13)
at _startMicrotaskLoop (http://localhost:65437/dart_sdk.js:40714:13)
at http://localhost:65437/dart_sdk.js:36191:9
如何解决这个问题?
更新
我在 Web 平台上 运行 时出现此错误,但在 Mobile 上 运行 时出现此错误& Desktop 平台我没有错误。我认为问题出在安全性或类似 CORS
问题出在 CORS 上。我需要通过在端点传递 CORS 参数来启用 CORS
@http.route('/my/route', auth='public' ,cors='*')
def my_route(self):