JQuery AJAX $.get()方法
來源:程序員人生 發布時間:2015-01-13 08:11:33 閱讀次數:4122次
AJAX = Asynchronous JavaScript and XML.
AJAX 是1種創建快速動態網頁的技術。
AJAX 通過在后臺與服務器交換少許數據的方式,允許網頁進行異步更新。這意味著有可能在不重載全部頁面的情況下,對網頁的1部份進行更新。
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⑵.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⑻"%>
<%
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 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中,測試1下。
【轉載使用,請注明出處:http://blog.csdn.net/mahoking】
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈