samedi 27 juin 2015

java ajax autocomplete not working

Ajax auto complete is not working. I debug the code and found that in my controller where I wrote the json line the debugger failed to debug there. I am new too this, plz help me out.

Controller

    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 CODE

$(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);
                        }
                });
        }
});
});
});

Aucun commentaire:

Enregistrer un commentaire