node表单提交

2018-02-21  本文已影响0人  温梦丽

今天发现表单提交中文,服务器得到的是编码后的数据。
提交英文,数字则不会被编码。

客户端:

//form.html:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="http://localhost:8082" method="post">
    用户名:<input type="text" name="user"><br>
    密码:<input type="password" name="pass"><br>
    <input type="submit" value="提交">
</form>
</body>
</html>

服务器端

//server.js
const  express=require("express");
var server=express();
server.listen(8082);
server.use(function (req,res,next) {

    var str="";
    req.on("data",function (data) {
        str+=data;
    });
   req.on("end",function () {
        req.body=str;
        next();
    });
});
server.use('/',function (req,res) {
    console.log(req.body);
});

打印数据如下:


image.png

这时候就需要使用decodeURIComponent()解码,可以将打印那行改写成

console.log( decodeURIComponent(req.body));

打印数据如下:


image.png
上一篇 下一篇

猜你喜欢

热点阅读