Home > Database > Mysql Tutorial > mysql-node.js解析input中文报错

mysql-node.js解析input中文报错

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 09:34:13
Original
986 people have browsed it

mysqlnode.js表单数据库

通过nodejs将html页面中的表单内容插入mysql数据库。当input文本框内输入的为英文或数字时完全正确,一旦输入中文,点击提交按钮,服务端显示错误"Incorrect String Value……'
现在检查,数据库所有的字符设置都已经为utf8。nodejs代码中也有req.setEncoding('utf8')。表单form标签中也有accept-charset="UTF-8"。node.js代码、html代码、浏览器编码全部为utf8。请问是哪里的错误导致nodejs不能解析input里的中文?

以下是我node.js的代码:

<code> var http=require('http');var mysql=require('mysql');var qs=require('querystring');var db=mysql.createConnection({    host:'127.0.0.1',    user:'root',    password:'12345',    database:'datanews'});var server=http.createServer(function(req,res){    switch (req.method) {        case 'POST':            switch (req.url) {                case '/':                    addUser(db,req,res);                    break;            }            break;    }});server.listen(3000,'127.0.0.1');function addUser(db,req,res) {    var newData='';    req.setEncoding('utf8');    req.on(        'data',        function(chunck){            newData+=chunck;        }    );    req.on(        'end',        function () {            var data=qs.parse(newData);            db.query(                "INSERT INTO userInfo(nickname,identify,password,gender,birthDay) "+                " VALUES(?,?,?,?,?)",                [data.nickname,data.identify,data.password,data.gender,data.birthDay],                function (err) {                    if (err) {                        throw err;                    }                    else{                        console.log("seccussful");                        var html='<p>注册成功</p>'                        toBrowser(res,html);                    }                }            )        }    )}function toBrowser(res,htmldata) {    res.setHeader('Content-Type','text/html;charset=utf8');    res.setHeader('Content-Length',Buffer.byteLength(htmldata));    res.end(htmldata);}</code>
Copy after login
Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template