ajax.html,比如我把jsp中的tom改为张三,然后在ajax.html中可以输入张三,显示数据。但是显示不出,我也知道是编码的问题,但是不知道做,附件是jquery.js
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>ajax.html</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <script type="text/javascript" src="/test/js/jquery-1.4.3.js"> </script> <style type="text/css"> body{font-size:13px} .divFrame{width:260px;border:solid 1px #666} .divFrame .divTitle{padding:5px;background-color:#eee} .divFrame .divTitle .clsShow{font-size:14px} .btn {border:#666 1px solid;padding:2px;width:80px; } </style> <script type="text/javascript"> $(function(){ $("#Button1").click(function(){ //按钮单击事件 //打开文件,并通过回调函数返回嗠器响应后的数据 $.post("UserInfo.jsp",{name:encodeURI($("#txtName").val()),sex:encodeURI($("#selSex").val())} ,function(data){ $("#divTip").empty().html(data); }) }) }) </script> </head> <body> <div class="divFrame"> <div class="divTitle"> name:<input type="text" id="txtName"/> <br/> sex:<input type="text" id="selSex"/><br/> <input id="Button1" type="button" class="btn" value="获取数据"/> </div> <div class="divContent"> <div id="divTip"></div> </div> </div> </body> </html>
UserInfo.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'UserInfo.jsp' starting page</title> </head> <body> <% request.setCharacterEncoding("UTF-8"); String strName=request.getParameter("name"); String strSex=request.getParameter("sex"); String strHTML="<DIV CLASS='clsShow'>";//初始化保存内容变量 if(strName.equals("tom") && strSex.equals("man")) { strHTML+="姓名:tom<br/>性别:man<br/>邮箱:abc@gmail.com<br/><hr/>"; } strHTML+="</div>"; response.setCharacterEncoding("text/html;charset=UTF-8"); response.setContentType("text/html;charset=UTF-8"); response.getWriter().print(strHTML); %> </body> </html>