1

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();
 alert(txt1)
 $.ajax({
            url : 'http://localhost:8080/searchengine/SearchDataServlet',
            data : 'val='+txt1,
            type : 'GET',
            success : function(response) {
                alert("Success");
                // 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;

    System.out.println(searchkey);
    SearchParser searchparser = new SearchParser();
    searchparser.searchData(searchkey);


    PrintWriter output = response.getWriter();
    output.println("successs");


}

}

1 Answer 1

1

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

see this for example

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for you reply but it doesn't response success
@Mayur wait for 5 minutes, I am busy with my work.i will help you as soon i complete my work
@Mayur Have you configured your web.xml properly?
@Mayur replace this http://localhost:8080/searchengine/SearchDataServlet with SearchDataServlet and tell me
i got it i was getting the error bcoz i am calling ajax from another port instead 8080 and also access.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.