问题描述: 在前台(jsp)用ajax提交数据, 后台(servlet)处理后返回"1"或者"0". 可是前台获得的返回数据是长度为3的字符串, 即"1__"或"0__"(_表示一个空白字符). 环境: tomcat-7.0.29 + MySQL 5.5 + jquery 1.7 其他: 以为是UTF-8 + BOM的问题, 可是把所有文件都改为UTF-8了,问题依旧. 这是后台处理请求的Validate.java文件
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; public class Validate extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String user_name = new String(request.getParameter("userName").getBytes("ISO-8859-1"), "UTF-8"); String user_pwd = new String(request.getParameter("password").getBytes("ISO-8859-1"), "UTF-8"); PrintWriter out = response.getWriter(); String driver = "com.mysql.jdbc.Driver"; String dbUser = "root"; String dbPwd = "3306"; String db = "appbase"; String url = "jdbc:mysql://localhost:3306/" + db + "?user=" + dbUser + "&password=" + dbPwd; ResultSet rs = null; String sqlString = "select * from client where login_account='" + user_name + "' and password='" + user_pwd + "'"; try { Class.forName(driver).newInstance(); Connection conn = DriverManager.getConnection(url); Statement ps = conn.createStatement(); rs = ps.executeQuery(sqlString); if(rs.next()) out.println("1"); else out.println("0"); } catch(ClassNotFoundException e) { out.println("ClassNotFoundException"); } catch(SQLException ex) { out.println("SQLException"); } catch(Exception exe) { out.println("OtherException"); } } }这是前台ajax提交数据的代码:
$(document).ready(function() { $("#_submit").click(function() { var user_name = $("#_user_name").val(); var user_pwd = $("#_user_pwd").val(); $.ajax({ url: "login/validate", data: { userName: user_name, password: user_pwd }, type: "GET", datatype: "html", success: function(data){ alert(data.length); } }); }); });
是不是输出时换行的问题,多了\r\n######
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。