node.js - 无法执行发布
问题描述
我想执行从客户端到服务器的发布。在客户端我使用flutter,在服务器端使用nodejs。问题是我无法执行后期操作。但是如果使用参数值对路由进行硬编码,我可以将数据发送到服务器。
静态字符串 urlLogin = API.url + '/authentication/api/login/ADMIN/12345';
但是当我使用下面的代码并使用 body 属性时,无法将数据发送到服务器。
静态字符串 urlLogin = API.url + '/authentication/api/login/';
我的客人是参数有问题..也许......我不知道我犯了什么错误。希望能帮助我解决这个问题。提前谢谢你。
错误
<pre>Cannot POST /authentication/api/login</pre>
</body>
</html>
FormatException: Unexpected character (at character 1)
(客户网站)
LoginPageForm.dart
class LoginPageForm extends StatefulWidget {
LoginPageForm({Key key}) : super(key: key);
@override
_LoginPageFormState createState() => _LoginPageFormState();
}
class _LoginPageFormState extends State<LoginPageForm> {
TextEditingController _idController = TextEditingController();
TextEditingController _passwordController = TextEditingController();
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Form(
key: _formKey,
child: Column(
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width,
child: TextFormField(
cursorColor: Colors.white,
style: TextStyle(color: Colors.white),
controller: _idController,
decoration: InputDecoration(
labelText: "No ID",
labelStyle: TextStyle(color: Colors.white),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white))),
validator: (value) {
if (value.isEmpty) {
return "Sila masukkan no id pengguna";
}
return null;
},
),
),
Container(
width: MediaQuery.of(context).size.width,
child: TextFormField(
cursorColor: Colors.white,
style: TextStyle(color: Colors.white),
controller: _passwordController,
decoration: InputDecoration(
labelText: "Kata Laluan",
labelStyle: TextStyle(color: Colors.white),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white))),
validator: (value) {
if (value.isEmpty) {
return "Sila masukkan kata laluan..";
}
return null;
},
),
),
RaisedButton.icon(
onPressed: () {
var login = new LoginPost(
userNo: _idController.text,
password: _passwordController.text);
//perform post
LoginRepository.login(login.toMap()).then((response) {
print(response);
});
},
icon: Icon(Icons.keyboard_arrow_right),
label: Text("Daftar Masuk")),
],
),
);
}
}
loginRepository.dart
class LoginRepository {
static String urlLogin = API.url + '/authentication/api/login/';
//static String urlLogin = API.url + '/authentication/api/login/ADMIN/12345';
static Future<LoginResponse> login(Map<String, dynamic> loginPost) async {
try {
final response =
await http.post(Uri.encodeFull(urlLogin), body: loginPost);
print(response.statusCode);
// if (response.statusCode == 200) {
print(response.body.toString());
return LoginResponse.fromMap(json.decode(response.body));
// } else {
// return null;
// }
} catch (e) {
print(e);
return null;
}
}
}
(服务器站点)
router.post('/api/login/:userNo/:password',(req,res)=>{
var userNo = req.params.userNo;
var password = req.params.password;
let Status;
const formData = {
userNo: userNo,
password : password
}
});
解决方案
发帖body
不会给你一个 URL 路径,例如id = 1:
- 用网址发帖:
await http.post('user/' + id);
https://用户/1
- 带正文的帖子:
await http.post('user/', body:{'id': id});
https://user?id=1
因此,在使用颤振发出请求之前,您应该指定在服务器端使用哪一个。
推荐阅读
- opencv - 立体匹配器 opencv 中的计算函数 - 获取视差
- bash - Bash 重定向。stdout 到 shell 和 stdout+stderr 到文件
- php - 仅将字节更改为 GB
- javascript - 用户在一个时间范围内不能多次点击
- ssl - 在浏览器中访问 url 时出现 SEC_ERROR_REVOKED_CERTIFICATE 错误
- ios - Xcode iPhone X - 如何返回黑带
- javascript - connect() 依赖于顶部连接组件的深度连接组件
- javascript - 如何使用 createElement 渲染函数在 javascript 中编写 vue .sync
- pentaho - 替换字符串,删除停用词
- android - 我无法在 Android SqlLite 中存储 MySql 时间戳