流浪天下
背包行走,流浪天下
流浪天下
当前位置: 首页 > 学习 > 正文

Ajax返回中文数据乱码的解决方法

最近在用到Ajax.request时,发现从ASP返回的参数如果是中文数据就很容易产生乱码,但是我的页面已经是UTF-8编码了,在网上找了很多资料,最后发现在响应页的页头加上如下代码就可以了

<% Response.CodePage=65001%>
<% Response.Charset="UTF-8" %>

当然如果你用的是gb2312就加入这句:

Response.Charset = "gb2312"

网上有朋友提供如下代码:

当通过new Ajax.Request()方法提交带有中文参数的数据时,提交前应该对中文参数进行处理,然后才可以在服务器端读出正确的gb2312编码数据.

如提交留言,然后加载新列表:

      var comment = document.getElementById('text');
      var request = "";

       comment = escape(comment.value);
       request = request.concat('text=');
       request = request.concat(comment);
       new Ajax.Request('comment_save.asp?id=100', {method: 'post', postBody: request, onSuccess: function() {
       new Ajax.Updater('Guest_List', 'getcomment.asp?id=100', {method:'get', postBody: "", asynchronous:true, evalScripts:true, onSuccess: function() {;}});  }});

在PHP中可以加上这句:

 <?header("Content-Type: text/html; charset=gb2312");?>

如果还是不行,试试这句

< ?echo iconv ("gb2312", "utf-8", $Post);? >

相关推荐

《Ajax返回中文数据乱码的解决方法》等您坐沙发呢!

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

Gravatar

? razz sad evil ! smile oops grin eek shock ??? cool lol mad twisted roll wink idea arrow neutral cry mrgreen