calling servlet from php page using ajax

i am trying to get response from servlet page and displaying alert on success. but its show me error always. i am not able to figure it out.

My ajax code:

$(document).ready(function() {
$("#srch").click(function() {
 var txt1 = $("#store-qsearch").val();
            url : 'http://localhost:8080/searchengine/SearchDataServlet',
            data : 'val='+txt1,
            type : 'GET',
            success : function(response) {
                // create an empty div in your page with some id
            error: function(){ alert("error");


My servlet code:

public class SerachDataServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String searchkey = request.getParameter("val").toString();
    SearchInput searchinput = new SearchInput();
    searchinput.searchkeys = searchkey;

    SearchParser searchparser = new SearchParser();

    PrintWriter output = response.getWriter();




change this line data : 'val='+txt1, to data: { val: txt1},

see this for example

Leave a Reply

Your email address will not be published. Required fields are marked *