JQuery AJAX $.get()方法,jquery.get
AJAX = Asynchronous JavaScript and XML.
AJAX 是一種建立快速動態網頁的技術。
AJAX 通過在後台與伺服器交換少量資料的方式,允許網頁進行非同步更新。這意味著有可能在不重載整個頁面的情況下,對網頁的一部分進行更新。
JQuery指令碼庫裡所提供的AJAX提交的方法有很多,但主要的方法有$.get(),$.post(),$.ajax()。其中$.ajax()是前兩種方法的底層實現,可以提供比前兩者更多的屬性與參數設定,如果需要進階的設定使用,建議使用$.ajax()方法。
【轉載使用,請註明出處:http://blog.csdn.net/mahoking】
學習$.get()方法
學習$.post()方法
學習$.ajax()方法
$.get()方法
get() 方法通過遠程 HTTP GET 請求載入資訊。
文法:
$.get(url,data,success(response,status,xhr),dataType)
注釋:
url 必需。規定將請求發送的哪個 URL。
data 可選。規定連同請求發送到伺服器的資料。
success(response,status,xhr) 可選。規定當請求成功時啟動並執行函數。
額外的參數:
response - 包含來自請求的結果資料
status - 包含請求的狀態
xhr - 包含 XMLHttpRequest 對象
dataType 可選。規定預期的伺服器響應的資料類型。預設執行智能判斷(xml、json、script、text、html等)。
示範案例:
1、 建立Web項目JQueryAjax。
2、 在WebRoot下建立js/jquery檔案目錄,添加jquery-2.1.1.js
3、 建立Servlet(AjaxGetServlet)。如下:
public class AjaxGetServlet 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_get.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 ajaxGet(){$.get("servlet/AjaxGetServlet", //url地址{userName:$("#userName").val(),age:$("#age").val()},function(data){ //回傳函數alert(data);},"text")}</script> </head> <body> <br> <div class="text_align-center">JQuery AJAX $.get()方法提交示範</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="$.get()方法提交"/><br/></form></div> <hr /> </body></html>
5、將項目部署到Tomcat中,測試一下。
【轉載使用,請註明出處:http://blog.csdn.net/mahoking】