简体   繁体   English

Jquery发布到Servlet

[英]Jquery Post to Servlet

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. 是null并且我得到null引用exeption。 What am I doing wrong? 我究竟做错了什么?

I think you want request.getParameter("orderId") . 我想你想要request.getParameter("orderId") Attributes are only for server side use while processing the request. 属性仅供服务器端处理请求时使用。 Parameters contain request data from the client side. 参数包含来自客户端的请求数据。

You should use getParameter method instead of getAttribute. 您应该使用getParameter方法而不是getAttribute。

request.getParameter("orderId")

getParameter() will retrieve a value that the client has submitted. getParameter()将检索客户端已提交的值。 Where as you should use getAttribute() when you submit the request to another resource (server side). 当您将请求提交给另一个资源(服务器端)时,应该使用getAttribute()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM