0
我試圖讓自動完成html頁面上的搜索框顯示結果使用從birdws.java Web服務提供的JSON。每次我在文本框中輸入文本時,都會給我一個錯誤。 Birdws提供的JSON文本在JSONLint.com上進行了驗證,我不知道我在做什麼錯誤,我一直在盯着這個小時。我不知道該怎麼辦。JQuery.ajax錯誤。無法找出問題
這是HTML文件:
<!DOCTYPE html>
<html>
<head>
<title>Auto Complete</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="scripts/jquery-2.1.3.min.js" type="text/javascript"></script>
<script>
// $ <-- means you are accessing jQuery
$('document').ready(function(){
$('#txtBirdComName').keyup(function(){
var url;
var typedText = $('#txtSearch').val();
url = "rest/birdws?birdComName=" + typedText;
getDataFromWebService(url);
});
});
function getDataFromWebService(targetUrl){
jQuery.ajax({
type: "GET",
url: targetUrl,
contentType: "application/json; charset=UTF-8",
dataType: "json",
success: function(data, status, jqXHR){
alert("win!");
},
error: function(jqXHR, status) {
alert("Something went wrong!" + "\n" +
"Status: " + status + "\n"); // <---- keeps erroring
console.log(jqXHR.toString());
}
});
}
</script>
</head>
<body>
<div id="mainContent">
<input type="text" name="txtBirdComName" id="txtBirdComName" value="" />
<br><br>
<table id="tblBirds" border="1">
</table>
</div>
</body>
</html>
這是Web服務:
package edu.pitt.rest;
import edu.pitt.core.Bird;
import edu.pitt.utilities.DbUtilities;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONObject;
/**
*
* @author CS_User
*/
@WebServlet(name = "birdws", urlPatterns = {"/rest/birdws"})
public class birdws extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/json; charset=UTF-8");
PrintWriter out = response.getWriter();
Bird bird = null;
String birdComName = "";
if(request.getParameter("birdComName") != null){
birdComName = request.getParameter("birdComName");
}
DbUtilities db = new DbUtilities();
String sql = "SELECT * FROM birds ";
if(!birdComName.equals("")){
sql += "WHERE birdComName LIKE '" + birdComName + "%' ";
sql += "LIMIT 20";
}
JSONArray fullBirdList = new JSONArray();
ResultSet rs;
try {
rs = db.getResultSet(sql);
// use this to get the length of rs
int rowcount = 0;
if (rs.last()) {
rowcount = rs.getRow();
rs.beforeFirst(); // use this to place rs back to the front
}
//out.printf("[");
while(rs.next()){
bird = new Bird(Integer.parseInt(rs.getString("birdID")));
JSONObject birdJSON = bird.toJSON();
if(rs.getRow() != rowcount){
out.printf(birdJSON.toString() + ",");
}else{
out.printf(birdJSON.toString());
}
}
//out.printf("]");
} catch (SQLException ex) {
Logger.getLogger(birdws.class.getName()).log(Level.SEVERE, null, ex);
}
db.closeDbConnection();
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
警報(thrownError); //不顯示 – user3044394 2015-04-03 18:39:01
嘗試刪除上述提醒,看看它是否工作提醒(「出錯了!」+「\ n」+ 「狀態:」+狀態+「\ n」); // <----不要求錯誤 – chetank 2015-04-03 18:40:39
console.log(thrownError); 說:未捕獲的ReferenceError:thrownError沒有定義 – user3044394 2015-04-03 18:40:55