java ajax autocomplete无法正常工作

时间:2015-06-27 15:21:40

标签: java jquery ajax servlets

Ajax自动完成无法正常工作。我调试代码,发现在我编写json行的控制器中,调试器无法调试。我也是新人,请帮助我。

控制器

    response.setContentType("application/json");
        try {
                String term = request.getParameter("term");
                System.out.println("Data from ajax call " + term);

                AutoData a = new AutoData();
                a.setName(term);

                DataDao d = new DataDao();
                List<AutoData> data = d.getData();


                String searchList = new Gson().toJson(data);
                response.getWriter().write(searchList);
        } catch (Exception e) {
                System.err.println(e.getMessage());
        }
}

DataDAO

public class DataDao {
        private String sql;
        private ResultSet rs;


        public List<AutoData> getData(){
            List<AutoData> aData = new ArrayList<AutoData>();
            try{
            sql = "select * from userdetails";
            rs = DBConnection.executeQuery(sql);
            while(rs.next()){

                AutoData a = new AutoData();
                a.setName(rs.getString("userid"));
                aData.add(a);
            }
            }
            catch(Exception e){
                System.out.println(e.getMessage());
            }
            return aData;





}
}

AJAX代码

$(document).ready(function() {
        $(function() {
                $("#search").autocomplete({     
                source : function(request, response) {
                $.ajax({
                        url : "AutoController",
                        type : "GET",
                        data : {
                                term : request.term
                        },
                        dataType : "json",
                        success : function(data) {
                                response(data);
                        }
                });
        }
});
});
});

1 个答案:

答案 0 :(得分:0)

我发现我没有在正确的文件夹中添加gson api。因此,在添加API时请确保更好。