JavaScript的Ajax以及中文乱码问题
2017-08-17 10:43
459 查看
本节实现js的Ajax前台传送文字,后台接收并打印然后将受到的文字传到前台并提示的过程。
在写这段代码的时候遇到了乱码问题,解决方案是:在tomcat 的server.xml文件中
加上URIEncoding=”UTF-8”
问题解决
Ajax.jsp代码
Ajax.java部分代码:
在写这段代码的时候遇到了乱码问题,解决方案是:在tomcat 的server.xml文件中
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
加上URIEncoding=”UTF-8”
问题解决
Ajax.jsp代码
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Ajax</title> <script type="text/javascript"> function loadXMLDoc() { //创建XMLHttpRequest 对象或者ActiveX 对象 var xmlrequest; if(window.XMLHttpRequest){ xmlrequest = new XMLHttpRequest(); }else if(window.ActiveXObject){ try{ xmlrequest = new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try{ xmlrequest = new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){ } } } var account = "大大"; //使用open方法,第三个参数不写默认true即默认异步 xmlrequest.open("POSt",'http://localhost:8080/showUsers/Ajax?account='+account,true); //发送请求,先有open再有send后台才能接收 xmlrequest.send(); //监视函数,(onreadystatechange )存储函数(或函数名),每当 readyState 属性改变时,就会调用该函数。 //当readyState 返回4为请求已完成,且响应已就绪, //当status 返回200 servlet响应正确 xmlrequest.onreadystatechange = function processResponse(){ //响应完成 if(xmlrequest.readyState == 4){ //响应正常 if(xmlrequest.status == 200){ var head = xmlrequest.responseText; alert(head); } } }; } </script> </head> <body> <div id="myDiv"><h3>Let AJAX change this text</h3></div> <button type="button" onclick="loadXMLDoc()">Change Content</button> <%=request.getParameter("account") %> </body> </html>
Ajax.java部分代码:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String account = request.getParameter("account"); System.out.println(account); response.setContentType("text/html;charset=UTF-8"); request.setAttribute("account", account); PrintWriter writer = response.getWriter(); writer.write(account); writer.flush(); writer.close(); }
相关文章推荐
- 记录问题:ajax返回对象到页面问题以及数据中文乱码问题
- JavaScript传中文值给Action出现乱码问题解决和返回ajax 返回 输出 json 乱码
- ajax中文数据传输乱码问题以及zend framework escape()调用
- 关于ajax中文乱码问题
- 中文在ajax中的乱码问题
- 用ajax传递json到前台中文出现问号乱码问题的解决办法
- Jira饼图上中文乱码,显示为“口”的解决办法,以及问题附件中文名时无法打开终极办法
- 简易解析ajax,javascript-XMLHttpRequest,解决乱码问题
- Ubuntu14.04中文乱码以及搜狗输入法无法正常输出中文的问题
- Extjs4.2 ajax请求url中传中文参数乱码问题
- struts2重定向时传递参数的实现!以及传递参数为中文时的乱码问题解决!
- 关于AJAX传参中文乱码问题
- tomcat发布项目 ajax方式post请求返回数据中文乱码问题(Eclipse中tomcat正常)
- 关于ajax中文乱码问题
- jQuery-Ajax提交数据中文乱码问题的解决方案
- Ajax中文乱码问题解决方案
- Javascript与ASP.NET(一般处理程序)关于URL(get方式)传递中文乱码问题的解决方案
- MySQL的字符集以及中文乱码问题
- PHP数组传递给JavaScript以及json_encode的gbk中文乱码的解决
- Java中scanner.next()键盘输入中文乱码以及转码乱码的问题