今天自己做个项目碰到了传中文,接受时乱码,如下:
发送:
之后在在需要传中文的地方加上escape,解决!
相关解决办法如下:
解决AJAX传送中文会导致乱码的问题
//
如果传送参数是直接赋予的,就会产生乱码!
http_request.open( " POST " ,url, true );
http_request.setRequestHeader('Content - Type', 'application / x - www - form - urlencoded');
http_request.send( " action= " + strName + " &val= " + val); // 如果val的值为中文,则产生乱码
// 解决方法很简单:使用javascript中的escape(string) 函数
http_request.open( " POST " ,url, true );
http_request.setRequestHeader('Content - Type', 'application / x - www - form - urlencoded');
http_request.send( " action= " + strName + " &val= " + escape(val)); // val的值为中文不会产生乱码
http_request.open( " POST " ,url, true );
http_request.setRequestHeader('Content - Type', 'application / x - www - form - urlencoded');
http_request.send( " action= " + strName + " &val= " + val); // 如果val的值为中文,则产生乱码
// 解决方法很简单:使用javascript中的escape(string) 函数
http_request.open( " POST " ,url, true );
http_request.setRequestHeader('Content - Type', 'application / x - www - form - urlencoded');
http_request.send( " action= " + strName + " &val= " + escape(val)); // val的值为中文不会产生乱码
关于在AJAX中GET回的ResponseText中文乱码的最简解决办法
用AJAX 来GET回一个页面时,RESPONSETEXT里面的中文多半会出现乱码,这是因为xmlhttp在处理返回的responseText的时候,是把 resposeBody按UTF-8编码进解码考形成的,如果服务器送出的确实是UTF-8的数据流的时候汉字会正确显示,而送出了GBK编码流的时候就乱了。解决的办法就是在送出的流里面加一个HEADER,指明送出的是什么编码流,这样XMLHTTP就不会乱搞了。
PHP:header('Content-Type:text/html;charset=GB2312');
ASP:Response.Charset("GB2312")
JSP:response.setHeader("Charset","GB2312");