flutter - 使用 HTTP POST 进行登录身份验证
问题描述
我正在尝试使用HTTP POST实现简单的登录身份验证,但我response.statusCode == 200
总是这样做,它允许我转到下一页。
这是我的简单身份验证功能:
Future<void> login() async {
if (passwordontroller.text.isNotEmpty && emailController.text.isNotEmpty) {
var response = await http.post(Uri.parse("http://myAPI/login"),
body: ({
'LoginId': emailController.text,
'Password': passwordontroller.text
}));
if (response.statusCode == 200) {
print("Correct");
Navigator.push(context, MaterialPageRoute(builder: (context)=> Second()));
} else {
print("Wronggooooooooooooooooooooooooooo");
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text("Invalid credentials")));
}
} else {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text("Blank field is not allowed")));
}
}
我对我的 Json 参数正文有疑问。
这里是我的带有 emailController 和 passwordController 的 TextFormField:
TextFormField(
controller:
emailController, //==========================================
decoration: InputDecoration(
labelText: "Email",
border: OutlineInputBorder(),
suffixIcon: Icon(Icons.email)),
),
SizedBox(
height: 10,
),
TextFormField(
controller:
passwordontroller, //=========================================
obscureText: true,
decoration: InputDecoration(
labelText: "Password",
border: OutlineInputBorder(),
suffixIcon: Icon(Icons.password)),
),
SizedBox(
height: 45,
),
OutlinedButton.icon(
onPressed: () {
login();
},
解决方案
statusCode 200 仅表示您从服务器获得了成功的响应。这很可能不会说明您发送的数据的实际身份验证状态,您可以“很可能”在响应正文中找到。
推荐阅读
- html - CSS:调整 div 大小以适应绝对定位的内容
- mysql - DB Design - 按类别显示表格
- assembly - 在 MBR 中指定分区表时,磁盘映像显示为不可引导
- google-api - 谷歌日历限制问题即使记帐已注册
- amazon-ec2 - 如何使用标记方法根据应用代码调用 ec2 实例列表
- bndtools - 使用 bnd 插件的 Eclipse maven 工作区太慢
- python - 在 VSCode 中分别拥有用户设置和项目设置
- javascript - 导出 webpack.config.js 中的全局函数以在 typescript 中使用
- c++ - 如何部署 C 静态库:头文件 + c 文件或目标文件?
- c# - 错误:不允许文件操作。拒绝访问路由