標籤: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)。如下:
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-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】
JQuery AJAX $.post()方法