JQuery AJAX $.post()方法
來源:程序員人生 發(fā)布時(shí)間:2015-01-12 08:16:36 閱讀次數(shù):2855次
AJAX = Asynchronous JavaScript and XML.
AJAX 是1種創(chuàng)建快速動(dòng)態(tài)網(wǎng)頁的技術(shù)。
AJAX 通過在后臺(tái)與服務(wù)器交換少許數(shù)據(jù)的方式,允許網(wǎng)頁進(jìn)行異步更新。這意味著有可能在不重載全部頁面的情況下,對(duì)網(wǎng)頁的1部份進(jìn)行更新。
JQuery腳本庫里所提供的AJAX提交的方法有很多,但主要的方法有$.get(),$.post(),$.ajax()。其中$.ajax()是前兩種方法的底層實(shí)現(xiàn),可以提供比前二者更多的屬性與參數(shù)設(shè)置,如果需要高級(jí)的設(shè)置使用,建議使用$.ajax()方法。
【轉(zhuǎn)載使用,請(qǐng)注明出處:http://blog.csdn.net/mahoking】
學(xué)習(xí)$.get()方法
學(xué)習(xí)$.post()方法
學(xué)習(xí)$.ajax()方法
$.post()方法
post() 方法通過 HTTP POST 要求從服務(wù)器載入數(shù)據(jù)。
語法:
$.post(url,data,success(data, textStatus, jqXHR),dataType)
注釋:
url 必須。規(guī)定把要求發(fā)送到哪一個(gè) URL。
data 可選。映照或字符串值。規(guī)定連同要求發(fā)送到服務(wù)器的數(shù)據(jù)。
success(data, textStatus, jqXHR) 可選。要求成功時(shí)履行的回調(diào)函數(shù)。
dataType 可選。規(guī)定預(yù)期的服務(wù)器響應(yīng)的數(shù)據(jù)類型。
默許履行智能判斷(xml、json、script、text、html等)。
演示案例:
1、 創(chuàng)建Web項(xiàng)目JQueryAjax。
2、 在WebRoot下創(chuàng)建js/jquery文件目錄,添加jquery⑵.1.1.js
3、 創(chuàng)建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");
}
/**
* 對(duì)要求提供返回?cái)?shù)據(jù)
* @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、 創(chuàng)建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){ //回傳函數(shù)
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、將項(xiàng)目部署到Tomcat中,測(cè)試1下。
【轉(zhuǎn)載使用,請(qǐng)注明出處:http://blog.csdn.net/mahoking】
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)