AJAX传送中文会导致乱码的问题



  1. 使用POST的时候:
  2. //如果传送参数是直接赋予的,就会产生乱码!
  3. http_request.open("POST",url,true);
  4. http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=gb2312');
  5. http_request.send("action="+strName+"&val="+val); //如果val的值为中文,则产生乱码
  6. //解决方法很简单:使用javascript中的escape(string) 函数
  7. http_request.open("POST",url,true);
  8. http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=gb2312');
  9. http_request.send("action="+strName+"&val="+escape(val)); //val的值为中文不会产生乱码
  10. 使用GET的时候:
  11. 1、在html标签meta中加入content="text/html; charset=gb2312" 确认浏览器解析时的编码.
  12. 2、确认服务器层面上的编码方式
  13. JSP:response.setHeader("Charset","GB2312");
【AJAX传送中文会导致乱码的问题】
转载于:https://blog.51cto.com/lya041/661421

    推荐阅读