node提交表单中文乱码问题解决
2019-02-18 本文已影响0人
扁扁的汤圆
表单:
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset="utf-8"/>
<title>02</title>
</head>
<body>
<form action="http://127.0.0.1:3000">
<input type="text" name="name"/></br>
<input type="text" name="age"/></br>
<input type="radio" name="sex" value="男"/>男
<input type="radio" name="sex" value="女"/>女</br>
<input type="submit" value="提交">
</form>
</body>
</html>
node:
var http = require("http");
var url = require("url");
http.createServer(function(req,res){
req.setEncoding('utf8');
res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
var urlObj = url.parse(req.url,true).query;
var name = urlObj.name;
var age = urlObj.age;
var sex = urlObj.sex;
res.end("服务器连接成功:" + name + age + sex);
}).listen(3000,'127.0.0.1');
加上req.setEncoding('utf8'); res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
即可