在gb2312编码网页中使用json数据,或用Ajax获取数据时常出现乱码,我今天就遇到了这样的问题, 经过转换编码最终解决.
问题描述: http://a.com/aa.php 中要调用 http://b.com/bb.php 页面中的内容, 这两个页面都是gb2312编辑, 显示中文出现乱码.
调用过程:
1. 在 http://b.com/bb.php 中把内容信息编码 'gb2312'->'utf-8' 然后用 json_encode() 转化为json数据 输出;
2. 新建页面 http://a.com/getB.php 来获取 http://b.com/bb.php 输出的json数据, 并用json_decode() 解码,对中文转码 'utf-8'->'gb2312', 生成编吗的静态缓存页面 bb.html, 再用 file_get_contents() 来获取 bb.html 页面内容, 并把内容转化编码 'gb2312'->'utf-8' 后输出 .
3. 在 http://a.com/aa.php 页面中用Ajax调用 http://a.com/getB.php , 把文件内容存储到服务器返回值 responseText 中并用 innerHtml()添加页面相应位置.
心得: 1. json_encode 只能对utf-8字符信息起作用.
2. 采用Ajax请求服务器数据时, responseText 获取的服务器信息也必须是utf-8编码的.
阅读(2280) | 评论(2) | 转发(0) |