Jquery发布到Servlet

dan*_*nik 6 java ajax post json servlets

我在客户端有以下代码:

      <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>
Run Code Online (Sandbox Code Playgroud)

服务器端:

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();
      }
    }
Run Code Online (Sandbox Code Playgroud)

我的

request.getAttribute("orderId")
Run Code Online (Sandbox Code Playgroud)

是null并且我得到null引用exeption.我究竟做错了什么?

Whi*_*g34 12

我想你想要的request.getParameter("orderId").属性仅供服务器端处理请求时使用.参数包含来自客户端的请求数据.