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处理中文乱码记录/nodejs+express error 413
原文:http://blog.51cto.com/314258/2067323