我有一個正在建立的應用程序,並且一年內沒有更改登入代碼,更新後我的flutter 登入頁面將請求OPTIONS且go API 伺服器回傳200。以前,伺服器中沒有出現 OPTIONS 日誌。這種情況發生在開發和生產。我的 Linux 用戶端登入正常。
我在這裡進行了 CORS 更改,但沒有效果。 GET 請求工作正常,但從來沒有 POST 請求到達伺服器。
這是我的錯:
ClientException: XMLHttpRequest error., uri=https://api.mydomain.com/login
這是我的伺服器端程式碼。
r := chi.NewRouter() r.Use(middleware.RequestID) r.Use(middleware.Logger) r.Use(middleware.Recoverer) r.Use(middleware.URLFormat) r.Use(render.SetContentType(render.ContentTypeJSON)) r.Use(cors.Handler(cors.Options{ AllowedOrigins: []string{"https://*, http://*"}, AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, AllowedHeaders: []string{"Accept", "Origin", "Authorization", "Content-Type", "X-CSRF-Token", "X-Requested-With"}, ExposedHeaders: []string{"Link"}, AllowCredentials: false, MaxAge: 300, // Maximum value not ignored by any of major browsers }))
這是我的顫振登入資訊:
Future<bool> login(String email, String password, String fcmToken) async { try { var response = await http.post( Uri.parse("$_baseUrl/login"), headers: <String, String>{ 'Content-Type': 'application/json; charset=UTF-8', }, body: jsonEncode( <String, String>{ 'business_email': email, 'password': password, 'token': fcmToken, }, ), ); if (response.statusCode == 200) { String jwt = response.body; await storage.write(key: 'jwt', value: jwt); return true; } } catch (e) { logger.d(e); return false; } return false; }
我的登入頁面
class Login extends StatefulWidget { const Login({ Key? key, }) : super(key: key); @override State<Login> createState() => _LoginState(); } class _LoginState extends State<Login> { @override void initState() { WidgetsBinding.instance.addPostFrameCallback( (_) => showSnackBar(context), ); super.initState(); } final GlobalKey<FormState> _formKey = GlobalKey(); final FocusNode _focusNodePassword = FocusNode(); final TextEditingController _controllerUserEmail = TextEditingController(); final TextEditingController _controllerPassword = TextEditingController(); bool _obscurePassword = true; bool isLoading = false; String? fcmToken = ''; @override Widget build(BuildContext context) { var model = LoginModel( authenticationService: Provider.of(context, listen: false), ); return Scaffold( body: Form( key: _formKey, child: SingleChildScrollView( padding: const EdgeInsets.all(30.0), child: Column( children: [ const SizedBox( height: 20, ), SizedBox( //height: 20, width: double.infinity, child: Text( 'exactCASE', textAlign: TextAlign.center, style: Theme.of(context).textTheme.headlineLarge, )), const SizedBox(height: 30), isLoading ? const Center(child: CircularProgressIndicator()) : Text( 'Login to your account', style: Theme.of(context).textTheme.labelMedium, ), const SizedBox(height: 20), TextFormField( controller: _controllerUserEmail, keyboardType: TextInputType.name, decoration: InputDecoration( labelText: 'Email', prefixIcon: const Icon(Icons.person_outline), border: OutlineInputBorder( borderRadius: BorderRadius.circular(10), ), enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(10), ), ), onEditingComplete: () => _focusNodePassword.requestFocus(), validator: (String? value) { if (value == null || value.isEmpty) { return "Please enter your email"; } return null; }, ), const SizedBox(height: 10), TextFormField( controller: _controllerPassword, focusNode: _focusNodePassword, obscureText: _obscurePassword, keyboardType: TextInputType.visiblePassword, decoration: InputDecoration( labelText: "Password", prefixIcon: const Icon(Icons.password_outlined), suffixIcon: IconButton( onPressed: () { setState(() { _obscurePassword = !_obscurePassword; }); }, icon: _obscurePassword ? const Icon(Icons.visibility_outlined) : const Icon(Icons.visibility_off_outlined)), border: OutlineInputBorder( borderRadius: BorderRadius.circular(10), ), enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(10), ), ), validator: (String? value) { if (value == null || value.isEmpty) { return "Please enter password."; } return null; }, ), const SizedBox(height: 30), Column( children: [ FilledButton( style: FilledButton.styleFrom( minimumSize: const Size.fromHeight(50), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(20), ), ), onPressed: () { if (_formKey.currentState?.validate() ?? false) { _login(context, model, _controllerUserEmail.text, _controllerPassword.text); } }, child: const Text("Login"), ), ], ), ], ), ), ), ); } @override void dispose() { _focusNodePassword.dispose(); _controllerUserEmail.dispose(); _controllerPassword.dispose(); super.dispose(); } Future<void> _login(BuildContext context, LoginModel model, String email, String password) async { final navigator = Navigator.of(context); if (defaultTargetPlatform != TargetPlatform.linux && defaultTargetPlatform != TargetPlatform.windows) { fcmToken = await FirebaseMessaging.instance.getToken(); } setState( () { isLoading = true; }, ); fcmToken ??= ''; var loginSuccess = await model.login(email, password, fcmToken!); if (loginSuccess) { navigator.pushNamed(RoutePaths.homeTabs); } else { setState( () { isLoading = false; final snackBar = SnackBar( content: const Text('Login Failed!'), action: SnackBarAction( label: 'OK', onPressed: () {}, )); ScaffoldMessenger.of(context).showSnackBar(snackBar); }, ); } } }
正確答案
根據 的文檔Access-Control-Allow-Origin 來源回應應該是單一URL(即請求者)或單個字元*
。
嘗試改變:
AllowedOrigins: []string{"https://*, http://*"},
至
AllowedOrigins: []string{"*"},
以上是flutter web 登入取得選項但不會發布的詳細內容。更多資訊請關注PHP中文網其他相關文章!

C 更適合需要直接控制硬件資源和高性能優化的場景,而Golang更適合需要快速開發和高並發處理的場景。 1.C 的優勢在於其接近硬件的特性和高度的優化能力,適合遊戲開發等高性能需求。 2.Golang的優勢在於其簡潔的語法和天然的並發支持,適合高並發服務開發。

Golang在实际应用中表现出色,以简洁、高效和并发性著称。1)通过Goroutines和Channels实现并发编程,2)利用接口和多态编写灵活代码,3)使用net/http包简化网络编程,4)构建高效并发爬虫,5)通过工具和最佳实践进行调试和优化。

Go語言的核心特性包括垃圾回收、靜態鏈接和並發支持。 1.Go語言的並發模型通過goroutine和channel實現高效並發編程。 2.接口和多態性通過實現接口方法,使得不同類型可以統一處理。 3.基本用法展示了函數定義和調用的高效性。 4.高級用法中,切片提供了動態調整大小的強大功能。 5.常見錯誤如競態條件可以通過gotest-race檢測並解決。 6.性能優化通過sync.Pool重用對象,減少垃圾回收壓力。

Go語言在構建高效且可擴展的系統中表現出色,其優勢包括:1.高性能:編譯成機器碼,運行速度快;2.並發編程:通過goroutines和channels簡化多任務處理;3.簡潔性:語法簡潔,降低學習和維護成本;4.跨平台:支持跨平台編譯,方便部署。

關於SQL查詢結果排序的疑惑學習SQL的過程中,常常會遇到一些令人困惑的問題。最近,筆者在閱讀《MICK-SQL基礎�...

golang ...

Go語言中如何對比並處理三個結構體在Go語言編程中,有時需要對比兩個結構體的差異,並將這些差異應用到第�...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

記事本++7.3.1
好用且免費的程式碼編輯器

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3漢化版
中文版,非常好用