使用ajax从php页面调用servlet

时间:2014-01-11 09:42:09

标签: java php ajax servlets

我正在尝试从servlet页面获取响应并显示成功警报。但它始终显示我的错误。我无法理解。

我的ajax代码:

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

            }
        });
});
                       });

我的servlet代码:

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 个答案:

答案 0 :(得分:1)

将此行data : 'val='+txt1,更改为data: { val: txt1},

请参阅example