2013-09-27 22 views
0

無法找到正確的方式從HttpRequest中獲得的參數值:無法找到正確的方式從HttpRequest中獲得的參數值:

這是我的JQuery文件:

$(document).ready(function() { 
var currBoard; 
var currCell; 
$(".cell").click(function() { 

    Cardboard = $ (this). attr ('name'); 
    currCell = $(this).attr('id'); 
    $.get('InGameServlet', function(responseText) { 
     $("#"+currCell).text(responseText); 

     alert("cell num:" + currCell + " Board: " + currBoard); 
    }); 
}); 

});

這是我的Servlet:

@WebServlet(name = "InGameServlet", urlPatterns = {"/InGameServlet"}) 
public class InGameServlet extends HttpServlet { 
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    response.setContentType("text/html;charset=UTF-8"); 

    printSign(request.getParameter("currBoard"), request.getParameter("currCell"),  response); 
} 

在調試模式下我看到我從請求得到的值是NULL!

我在做什麼錯了?

回答

1

您正在致電getAttribute()而不是getParameter()

請求參數作爲請求參數存儲在HttpServletRequest中。

使用

String value = request.getParameter("your parameter key"); 

這顯然取決於如果你的要求其實包含請求參數。 Here's how you do that with jQuery's get().

+0

喔對不起,這是測試我做了一個檢索他們...他們都返回空值。 – Gil404

1

你是不是從你的Ajax請求

$(".cell").click(function() { 

    Cardboard = $ (this). attr ('name'); 
    currCell = $(this).attr('id'); 
    $.get('InGameServlet?currBoard="+Cardboard+"currCell="+currCell', function(responseText) { //passing data as quesry param. 
     $("#"+currCell).text(responseText); 

     alert("cell num:" + currCell + " Board: " + currBoard); 
    }); 
}); 

然後在servlet的GET請求參數傳遞值

request.getParameter("currBoard"); 

,使其成爲,

printSign(request.getParameter("currBoard"),request.getParameter("currCell"),response); 
0

你不似乎是通過你的ajax調用中的任何參數。 另外參數是通過getParameter()方法而不是getValue()獲得的。

0

首先,你需要與發送請求的參數:

var data = { 
    currBoard: $(this).attr('name'), 
    currCell: $(this).attr('id') 
}; 
$.get('InGameServlet', data, function (responseText) { 
    $("#" + currCell).text(responseText); 

    alert("cell num:" + currCell + " Board: " + currBoard); 
}); 

然後用request.getParameter()代替getAttribute()

相關問題