标签:
1. jQuery.post( url, [data], [callback], [type] ) :使用POST方式来进行异步请求
<body> <div id="main"> <button id="myBut">Ajax获取数据</button> <div id="container"></div> </div> </body>1.2.2 style代码
<style type="text/css"> #main { margin: 0 auto; width: 400px; } #container { width: 400px; height: 300px; border: 1px dashed #666; text-align: center; line-height: 300px; } </style>
<script type="text/javascript" src="js/jquery-1.8.3.js"></script> <script type="text/javascript"> $(function() { $('#myBut').click(function() { $.post("JqueryAjaxServlet", { age : 18, name : "zhang" }, function(data, textStatus) { var container = $('#container'); var resultData = $.parseJSON(data); var age = resultData.age; var name = resultData.name; container.html("name:" + name + "," + "age:" + age); }); }); }); </script>1.2.4 JqueryAjaxServlet.java代码
public class JqueryAjaxServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String age = request.getParameter("age"); String name = request.getParameter("name"); String personJSON = "{\"name" + "\":\"" + name + "\"," + "\"age" + "\":" + age + "}"; System.out.println(personJSON); response.getWriter().write(personJSON); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }1.2.5 web.xml代码
<servlet> <servlet-name>JqueryAjaxServlet</servlet-name> <servlet-class>com.jquery.ajax.com.JqueryAjaxServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>JqueryAjaxServlet</servlet-name> <url-pattern>/JqueryAjaxServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
标签:
原文地址:http://blog.csdn.net/zbw18297786698/article/details/51334103