JQuery.ajax错误。无法弄清楚这个问题

时间:2015-04-03 18:29:44

标签: java jquery ajax json

我试图在自动完成html页面上获取此搜索框,以使用birdws.java Web服务提供的JSON显示结果。每当我在文本框中输入文本时,它都会给我一个错误。 Birdws提供的JSON文本在JSONLint.com上验证,我不知道我做错了什么,而且我已经盯着这几个小时了。我不知道该怎么做。

这是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>

这是网络服务:

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>

}

1 个答案:

答案 0 :(得分:1)

你可以使用下面的代码来查看错误是什么,它通常解释了什么是抛出的错误

error: function(jqXHR, status, thrownError) {
                    alert("Something went wrong!" + "\n" +
                            "Status: " + status + "\n"); // <---- keeps erroring
                    alert(thrownError);
                    console.log(thrownError);
                }