2015-04-03 103 views
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> 

} 

回答

1

您可以使用下面的代码,看看是什么错误,所以一般解释了什么是抛错误

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

警报(thrownError); //不显示 – user3044394 2015-04-03 18:39:01

+0

尝试删除上述提醒,看看它是否工作提醒(“出错了!”+“\ n”+ “状态:”+状态+“\ n”); // <----不要求错误 – chetank 2015-04-03 18:40:39

+0

console.log(thrownError); 说:未捕获的ReferenceError:thrownError没有定义 – user3044394 2015-04-03 18:40:55