java Jquery 发布到 Servlet
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5417359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery Post to Servlet
提问by danny.lesnik
I have the following code on client side:
我在客户端有以下代码:
<script src="http://code.jquery.com/jquery-1.5.js"></script>
<script>
$(document).ready(function() {
$("a").click(function() {
//var orderId = $("#orderId").val();
$.post("test", { orderId : "John"},
function(data) {
alert("Data Loaded: " + data);
});
});
});
</script>
Server side:
服务器端:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
PrintWriter writer = response.getWriter();
try{
String orderId = request.getAttribute("orderId").toString();
writer.write(orderId);
writer.close();
}
catch(Exception ex)
{
ex.getStackTrace();
}
}
my
我的
request.getAttribute("orderId")
is null and I'm getting null reference exeption. What am I doing wrong?
为空,我得到空引用例外。我究竟做错了什么?
回答by WhiteFang34
I think you want request.getParameter("orderId")
. Attributes are only for server side use while processing the request. Parameters contain request data from the client side.
我想你想request.getParameter("orderId")
。属性仅在处理请求时供服务器端使用。参数包含来自客户端的请求数据。
回答by Abdel Raoof
You should use getParameter method instead of getAttribute.
您应该使用 getParameter 方法而不是 getAttribute。
request.getParameter("orderId")
getParameter() will retrieve a value that the client has submitted. Where as you should use getAttribute() when you submit the request to another resource (server side).
getParameter() 将检索客户端提交的值。当您将请求提交给另一个资源(服务器端)时,您应该在哪里使用 getAttribute()。