JS传参中文乱码

在传参的时候给中文参数加上两个encodeURI即可,类似这样:

encodeURI(encodeURI(balanceUnit))

写法二

escape(encodeURI(tmplItem.hyzName))

接收函数用unescape(decodeURI(tmplItem.hyzName))

写法三

escape(encodeURI(decodeURIComponent(json.roleName)))

接收函数unescape(encodeURI(roleName))

在页面中显示时$('#roleName').text(unescape(decodeURI(roleName)));

还有一种是这样

最近使用jQuery遇到中文乱码问题,其实他的中文乱码就是因为contentType没有指定编码,只需在jQuery.js中搜索’contentType’ 然后在application/x-www-form-urlencoded后面加上; charset=UTF-8 最终变成contentType:"application/x-www-form-urlencoded; charset=UTF-8" 问题搞定。

以上写法都是在全局接收函数中没有解码的情况下实现的(之前没有去看项目中的解码函数,后悔中),所以时灵时不灵,最简单直接的办法就是直接在url上面转码解码,常用常灵

encodeURI("/Account_User/index.html?truckNumber=" + json.truckNumber+"&gmtCreate="+json.gmtCreate)

这里面车牌号是有中文字符的

解码的时候对这串url解码即可,简单直接,百用百灵。

var search = decodeURI(location.search).slice(1);
原文地址:https://www.cnblogs.com/ToBeBest/p/5892057.html