您的位置:首页 > Web前端 > Node.js

js处理中文乱码记录/nodejs+express error 413

2018-01-31 15:24 399 查看
在ajax提交数据时,由于中文全角字符等等问题导致服务器端接收到后显示乱码,具体表现为:\u00000\u000023等等(我本身想还原一下错误场景,尽然出现不了了,等下次在出现的时候,我在把乱码贴出来,下面具体讲解决方法):
细节原因不解释,自己可以去百度
乱码:

var n = $("#id").html();
$.ajax({
type: 'post',
dataType: 'json',
url: '/',
contentType: "application/x-www-form-urlencoded; charset=utf-8",
data: n,
success: function(data) {
console.log(data);
}
});

解决后:

var n = $("#id").html();
var _n = encodeURIComponent((encodeURI(n, "UTF-8")));

$.ajax({
type: 'post',
dataType: 'json',
url: '/',
contentType: "application/x-www-form-urlencoded; charset=utf-8",
data: _n,
success: function(data) {
console.log(data);
}
});

其实就加了encodeURIComponent encodeURI

在服务器端解码:

decodeURI(decodeURIComponent(escape(bodys)), "UTF-8")

顺便记录一下js在encode之后post提交到nodejs后端的时候,nodejs报413错误,其实修改方法很简单,修改下面两行即可(50m是不是有点大呀......):

app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  js中文乱码 u00000 413