国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > web前端 > htmlcss > JQuery AJAX $.post()方法

JQuery AJAX $.post()方法

来源:程序员人生   发布时间:2015-01-12 08:16:36 阅读次数:2589次

       AJAX = Asynchronous JavaScript and XML.
       AJAX 是1种创建快速动态网页的技术。
       AJAX 通过在后台与服务器交换少许数据的方式,允许网页进行异步更新。这意味着有可能在不重载全部页面的情况下,对网页的1部份进行更新。 
       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⑵.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"); } /** * 对要求提供返回数据 * @param request * @param response * @param method * @throws IOException */ 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⑻"%> <% 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⑵.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中,测试1下。


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



 

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生