我的javascript代码的post函数无法按预期工作
我有一个关于javascript的问题 我必须使用html文件头中javascript代码中的post函数将queryString变量中的数据发送到本地服务器中的autocom.jsp文件,然后使用回调函数(function(data){}从本地数据库获取数据 我的问题是,当我在html页面的输入字段中输入c时,网页上会显示testing ok,即使在我的数据库中,姓氏列(在数据库中称为last)中没有以字母c开头的数据。这不应该发生,因为data.length>0在这种特殊情况下不是真的 有人能帮我解决这个问题吗 谢谢 html文件为search.html,如下所示:我的javascript代码的post函数无法按预期工作,javascript,jquery,html,jsp,Javascript,Jquery,Html,Jsp,我有一个关于javascript的问题 我必须使用html文件头中javascript代码中的post函数将queryString变量中的数据发送到本地服务器中的autocom.jsp文件,然后使用回调函数(function(data){}从本地数据库获取数据 我的问题是,当我在html页面的输入字段中输入c时,网页上会显示testing ok,即使在我的数据库中,姓氏列(在数据库中称为last)中没有以字母c开头的数据。这不应该发生,因为data.length>0在这种特殊情况下不是真的 有人
<html>
<head>
<link href="styles.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript">
function checkSubmit (thisForm) {
if(thisForm.last.value=='') {
alert('Please Enter Last Name');
return false;
}
}
</script>
<script type="text/javascript">
function lookup(inputString) {
if(inputString.length == 0) {
out.println("testing go");
} else {
$.post("autocom.jsp", {queryString: "" +inputString+ ""}, function(data){
if(data.length >0) {
$('#suggestions').show();
$('#autoSuggestionsList').html(data);
$('#autoSug').html('<h1>testing ok</h1>');
}
});
}
}
</script>
</head>
<body>
<form method="post" action="search.jsp" onsubmit="return checkSubmit (this);">
Enter Last Name: <input type="text" name="last" value="" id="suggestions" id="autoSuggestionsList" onkeyup="lookup(this.value);" /><br>
<input type="submit" value="Submit">
</form>
<p id="autoSug"> </p>
<form method="post" action="home.html">
<input type="submit" value="Back To Home">
</form>
</body>
</html>
功能检查提交(此表单){
if(thisForm.last.value==''){
警报(“请输入姓氏”);
返回false;
}
}
函数查找(inputString){
如果(inputString.length==0){
out.println(“测试开始”);
}否则{
$.post(“autocom.jsp”,{queryString:“+inputString+”},函数(数据){
如果(data.length>0){
$(“#建议”).show();
$('#autoSuggestionsList').html(数据);
$('#autoSug').html('测试正常');
}
});
}
}
输入姓氏:
下面是autocom.jsp文件:
<%@ page language="java" import="java.sql.*" %>
<% response.setContentType("text/html");%>
<%
String str=request.getParameter("queryString");
String connectionURL = "jdbc:mysql://localhost/jsptest";
Connection con;
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(connectionURL, "root", "Dummy123"); // Get a Connection to the database
String sql = "SELECT last FROM employees WHERE last LIKE ' " +str+ " % ' LIMIT 10 "; //Add the data into the database
Statement stm = con.createStatement();
stm.executeQuery(sql);
ResultSet rs= stm.getResultSet();
while (rs.next ()){
out.println( "<i onclick='fill(" + rs.getString("last") + ");'>" +rs.getString("last")+ " </i>" );
}
%>
在
标记之外有新行,因此JSP将输出新行字符。这使得将放入数据中的字符串长度超过0
个字符。危险:您很容易在浏览器(控制台)网络中进行检查,键入单词时会得到什么响应?