jquery-ajax-$post方法

  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)。如下:

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. public class AjaxPostServlet extends HttpServlet {  
  2.   
  3.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  4.             throws ServletException, IOException {  
  5.   
  6.         retData(request, response, "GET");  
  7.     }  
  8.   
  9.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  10.             throws ServletException, IOException {  
  11.           
  12.         retData(request, response, "POST");  
  13.     }  
  14.   
  15.       
  16.     /** 
  17.      * 对请求提供返回数据 
  18.      * @param request 
  19.      * @param response 
  20.      * @param method 
  21.      * @throws IOException 
  22.      */  
  23.     private void retData(HttpServletRequest request, HttpServletResponse response,String method) throws IOException{  
  24.           
  25.         String userName = request.getParameter("userName");  
  26.         String age = request.getParameter("age");  
  27.         PrintWriter out = response.getWriter();  
  28.         out.print(method+":userName="+userName+",age="+age);  
  29.         out.flush();  
  30.     }  
  31. }  

4、 创建jquery_ajax_method_post.jsp。

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML>  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>JQuery AJAX</title>  
  13.     <meta http-equiv="pragma" content="no-cache">  
  14.     <meta http-equiv="cache-control" content="no-cache">  
  15.     <meta http-equiv="expires" content="0">      
  16.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  17.     <meta http-equiv="description" content="This is my page">  
  18.     <link rel="stylesheet" type="text/css" href="css/styles.css">  
  19.     <script type="text/javascript" src="js/jquery/jquery-2.1.1.js"></script>  
  20.     <script type="text/javascript">  
  21.       
  22.     //$.get()方法  
  23.     function ajaxPost(){  
  24.         $.post(  
  25.                 "servlet/AjaxPostServlet",      //url地址  
  26.                 {  
  27.                     userName:$("#userName").val(),  
  28.                     age:$("#age").val()  
  29.                 },  
  30.                 function(data){ //回传函数  
  31.                     alert(data);  
  32.                 },  
  33.                 "text")  
  34.     }  
  35.     </script>  
  36.   </head>  
  37.   <body>  
  38.   <br>  
  39.  <div class="text_align-center">JQuery AJAX $.post()方法提交演示</div>  
  40.   <hr />  
  41.     <div class="align-center">  
  42.         <form action="" method="post">  
  43.             姓名:<input type="text" name="userName" id="userName"/><br/>  
  44.             年龄:<input type="text" name="age" id="age"/><br/><br/>  
  45.             <input type="button" onclick="ajaxGet()" value="$.post()方法提交"/><br/>  
  46.         </form>  
  47.     </div>  
  48.   <hr />  
  49.   </body>  
  50. </html>  

5、将项目部署到Tomcat中,测试一下。


【转载使用,请注明出处:http://blog.csdn.Net/mahoking】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值