2014-01-11 162 views
1

我試圖從servlet頁面獲取響應並顯示成功警報。但它總是顯示我的錯誤。我無法弄清楚。使用ajax從php頁面調用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

改變這一行data : 'val='+txt1,data: { val: txt1},

看到這對example

+0

感謝您的答覆,但它不't迴應成功 – Mayur

+0

@Mayur它仍然顯示錯誤? – SpringLearner

+0

是的,它仍然顯示我的錯誤 – Mayur