最近在研究AJAX是,自己做了一个程序测试,发现在使用get方式提交时,如果参数中有中文的话,会出现乱码问题,而且取回的XMLHttpRequest.resopnseText中如果有中文,将其值在页面上显示后也是乱码。Google一下,发现大多是针对post方式提交的解决办法,
于是自己通过查看别人的文章,下来后自己调试,解决了get方式提交中文参数乱码的问题:
我的环境是表单页面index.html的编码是UTF-8:下面是我的代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Demo1.html</title>
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!-- HTTP 1.1 -->
<meta http-equiv="Cache-Control" content="no-store"/>
<!-- HTTP 1.0 -->
<meta http-equiv="Pragma" content="no-cache"/>
<!-- Prevents caching at the Proxy Server -->
<meta http-equiv="Expires" content="0"/>
<script language="javascript">
var httpRequest=null;
function createHttpRequest() {
if(window.XMLHttpRequest) {
return new XMLHttpRequest();
}
else if(window.ActiveXObject) {
request = new ActiveXObject("Microsoft.XMLHTTP");
if(!request) {
request = new ActiveXObject("Msxml2.XMLHTTP");
}
return request;
}
}
function ok() {
var url = "handel.jsp?name="+encodeURIComponent(document.form1.name.value);
httpRequest = createHttpRequest();
httpRequest.onreadystatechange = aa;
httpRequest.open("GET",url,true);
httpRequest.send(null);
}
function aa(){
if(httpRequest.readyState == 4) {
if(httpRequest.status == 200) {
document.getElementById("hh").innerText = httpRequest.responseText;
}
}
}
</script>
</head>
<body>
<form name="form1">
<input type="text" size="24" name="name">
<input type="button" value="提交" onclick="ok()"><br>
你输入了:<textarea rows="3" cols="20" readonly id="hh"></textarea>
</form>
</body>
</html>
在构造url是用javascript自带的encodeURIComponent方法将参数进行编码,下面是我的代码
var url = "handel.jsp?name="+encodeURIComponent(document.form1.name.value);
httpRequest = createHttpRequest();
httpRequest.onreadystatechange = aa;
httpRequest.open("GET",url,true);
httpRequest.send(null);
在目的地的handel.jsp页面(该页面显示编码也是UTF-8)上,先用iso-8859-1将request.getparameter()方法取回的参数值,转成字节串,
然后在用UTF-8将字节串转成字符串,就好了,下面是handel.jsp的代码:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String name = request.getParameter("name");
byte[] b = name.getBytes("iso-8859-1");
for(int i=0;i<b.length;i++) {
System.out.println(b[i]);
}
System.out.println("name:"+name);
System.out.println(new String(b,"UTF-8"));
response.setContentType("text/plain");
response.setHeader("Cache-Control","no-cache");
response.setHeader("Charset","UTF-8");
out.println("Welcome "+name);
%>
为了解决返回值XMLHttpRequest.resopnseText中文乱码的问题,需要设置下面这句话:
response.setHeader("Charset","UTF-8");
分享到:
相关推荐
JSP中 ajax的get请求的中文乱码问题的解决方法.pdf 学习资料 复习资料 教学资源
Ajax使用POST提交中文乱码问题.docx
Ajax中文乱码问题解决方案
NULL 博文链接:https://rengh8288.iteye.com/blog/391851
Ajax 中文乱码问题 Ajax 中文乱码问题 Ajax 中文乱码问题
主要解决在ajax中中文参数传递过程中,服务器接收到中文乱码的问题
在ajax中使用post方法,用常规的参数格式:param1=a1¶m2=a2 ,当参数长度过长时,依然提交不成功。比如我们经常这样写一个ajax的post请求: $.ajax({ type: "post", // post or get contentType:"application...
在js中对中文进行编码在java程序中进行解码即可解决中文乱码问题
解决ajax异步提交后台取值中文乱码问题,提供两种解决方法
jsp中 ajax的get请求的中文乱码问题的解决方法.docx
ajax乱码ajax乱码ajax乱码ajax乱码ajax乱码ajax乱码ajax乱码ajax乱码ajax乱码ajax乱码
ajax提交时,后台接收的参数出现中文乱码的解决方法,
Jquery解决异步提交ajax提交乱码问题
今天终于解决了AJAX的中文乱码问题,写篇文章来帮助一下有同样问题的朋友们。我的开发环境:XP, eclipse,使用GB18030编码。 当遇到这个问题时,到网上去查了好多文章,提到几种解决方案,如:全站UTF-8编码;请求头...
NULL 博文链接:https://rengh8288.iteye.com/blog/391838
在使用myeclipse过程中,我使用ajax同步请求传递参数到后台却出现中文乱码,这是我解决问题的代码,希望对您有所帮助
AJAX技术使用XMLHttpRequest对象传递参数的中文乱码问题
jquery+asp ajax 中文乱码问题解决文档
java认证_Ajax中文乱码问题及解决方法.doc
介绍了jsp中ajax的get请求的中文乱码问题的解决方法,有需要的朋友可以参考一下