AJAX = Asynchronous JavaScript and XML.
AJAX 是一种创建快速动态网页的技术。
AJAX 通过在后台与服务器交换少量数据的方式,允许网页进行异步更新。这意味着有可能在不重载整个页面的情况下,对网页的一部分进行更新。
jQuery脚本库里所提供的AJAX提交的方法有很多,但主要的方法有$.get(),$.post(),$.ajax()。其中$.ajax()是前两种方法的底层实现,可以提供比前两者更多的属性与参数设置,如果需要高级的设置使用,建议使用$.ajax()方法。
【转载使用,请注明出处:http://blog.csdn.NET/mahoking】
学习$.get()方法
学习$.post()方法
学习$.ajax()方法
$.post()方法
post() 方法通过 HTTP POST 请求从服务器载入数据。
语法:
$.post(url,data,success(data, textStatus, jqXHR),dataType)
注释:
url 必需。规定把请求发送到哪个 URL。
data 可选。映射或字符串值。规定连同请求发送到服务器的数据。
success(data, textStatus, jqXHR) 可选。请求成功时执行的回调函数。
dataType 可选。规定预期的服务器响应的数据类型。
默认执行智能判断(xml、json、script、text、html等)。
演示案例:
1、 创建Web项目JQueryAjax。
2、 在WebRoot下创建js/jquery文件目录,添加jquery-2.1.1.js
3、 创建Servlet(AjaxPostServlet)。如下:
- public class AjaxPostServlet extends HttpServlet {
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- retData(request, response, "GET");
- }
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- retData(request, response, "POST");
- }
-
-
-
-
-
-
-
-
-
- private void retData(HttpServletRequest request, HttpServletResponse response,String method) throws IOException{
-
- String userName = request.getParameter("userName");
- String age = request.getParameter("age");
- PrintWriter out = response.getWriter();
- out.print(method+":userName="+userName+",age="+age);
- out.flush();
- }
- }
4、 创建jquery_ajax_method_post.jsp。
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
-
- <!DOCTYPE HTML>
- <html>
- <head>
- <base href="<%=basePath%>">
-
- <title>JQuery AJAX</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <link rel="stylesheet" type="text/css" href="css/styles.css">
- <script type="text/javascript" src="js/jquery/jquery-2.1.1.js"></script>
- <script type="text/javascript">
-
- //$.get()方法
- function ajaxPost(){
- $.post(
- "servlet/AjaxPostServlet", //url地址
- {
- userName:$("#userName").val(),
- age:$("#age").val()
- },
- function(data){ //回传函数
- alert(data);
- },
- "text")
- }
- </script>
- </head>
- <body>
- <br>
- <div class="text_align-center">JQuery AJAX $.post()方法提交演示</div>
- <hr />
- <div class="align-center">
- <form action="" method="post">
- 姓名:<input type="text" name="userName" id="userName"/><br/>
- 年龄:<input type="text" name="age" id="age"/><br/><br/>
- <input type="button" onclick="ajaxGet()" value="$.post()方法提交"/><br/>
- </form>
- </div>
- <hr />
- </body>
- </html>
5、将项目部署到Tomcat中,测试一下。
【转载使用,请注明出处:http://blog.csdn.Net/mahoking】