JQuery之Ajax方法
1、使用load()方法異步請(qǐng)求數(shù)據(jù)
使用load()方法通過Ajax請(qǐng)求加載服務(wù)器中的數(shù)據(jù),并把返回的數(shù)據(jù)放置到指定的元素中,它的調(diào)用格式為:
load(url,[data],[callback])
參數(shù)url為加載服務(wù)器地址,可選項(xiàng)data參數(shù)為請(qǐng)求時(shí)發(fā)送的數(shù)據(jù),callback參數(shù)為數(shù)據(jù)請(qǐng)求成功后,執(zhí)行的回調(diào)函數(shù)。
例如,點(diǎn)擊“加載”按鈕時(shí),向服務(wù)器請(qǐng)求加載一個(gè)指定頁(yè)面的內(nèi)容,加載成功后,將數(shù)據(jù)內(nèi)容顯示在<div>元素中,并將加載按鈕變?yōu)椴豢捎谩?/p>
?
<body><div id="divtest"><div class="title"><span class="fl">我最愛吃的水果</span> <span class="fr"><input id="btnShow" type="button" value="加載" /></span></div><ul></ul></div><script type="text/javascript">$(function () {$("#btnShow").bind("click", function () {var $this = $(this);$("ul").html("<img src='Images/Loading.gif' alt=''/>").load( "http://www.imooc.com/data/fruit_part.html",function(){$this.attr("disabled", "true");});})});</script></body>?
2、使用getJSON()方法異步加載JSON格式數(shù)據(jù)
?
使用getJSON()方法可以通過Ajax異步請(qǐng)求的方式,獲取服務(wù)器中的數(shù)據(jù),并對(duì)獲取的數(shù)據(jù)進(jìn)行解析,顯示在頁(yè)面中,它的調(diào)用格式為:
jQuery.getJSON(url,[data],[callback])或$.getJSON(url,[data],[callback])
其中,url參數(shù)為請(qǐng)求加載json格式文件的服務(wù)器地址,可選項(xiàng)data參數(shù)為請(qǐng)求時(shí)發(fā)送的數(shù)據(jù),callback參數(shù)為數(shù)據(jù)請(qǐng)求成功后,執(zhí)行的回調(diào)函數(shù)。
例如,點(diǎn)擊頁(yè)面中的“加載”按鈕,調(diào)用getJSON()方法獲取服務(wù)器中JSON格式文件中的數(shù)據(jù),并遍歷數(shù)據(jù),將指定的字段名內(nèi)容顯示在頁(yè)面中。
?
<body><div id="divtest"><div class="title"><span class="fl">我最喜歡的一項(xiàng)運(yùn)動(dòng)</span> <span class="fr"><input id="btnShow" type="button" value="加載" /></span></div><ul></ul></div><script type="text/javascript">$(function () {$("#btnShow").bind("click", function () {var $this = $(this);$.getJSON("http://www.imooc.com/data/sport.json",function(data){$this.attr("disabled", "true");$.each(data, function (index, sport) {if(index==3)$("ul").append("<li>" + sport["name"] + "</li>");});});})});</script></body>?
?
3、使用getScript()方法異步執(zhí)行js文件
使用getScript()方法異步請(qǐng)求并執(zhí)行服務(wù)器中的JavaScript格式的文件,它的調(diào)用格式如下所示:
jQuery.getScript(url,[callback])或$.getScript(url,[callback])
參數(shù)url為服務(wù)器請(qǐng)求地址,可選項(xiàng)callback參數(shù)為請(qǐng)求成功后執(zhí)行的回調(diào)函數(shù)。
例如,點(diǎn)擊“加載”按鈕,調(diào)用getScript()加載并執(zhí)行服務(wù)器中指定名稱的JavaScript格式的文件,并在頁(yè)面中顯示加載后的數(shù)據(jù)內(nèi)容,
?
?
?
<body><div id="divtest"><div class="title"><span class="fl">我最喜歡的運(yùn)動(dòng)</span> <span class="fr"><input id="btnShow" type="button" value="加載" /></span></div><ul></ul></div><script type="text/javascript">$(function () {$("#btnShow").bind("click", function () {var $this = $(this);$.getScript("http://www.imooc.com/data/sport_f.js",function(){$this.attr("disabled", "true");});})});</script></body>?
?
4、使用get()方法以get方式從服務(wù)器獲取數(shù)據(jù)
使用get()方法時(shí),采用GET方式向服務(wù)器請(qǐng)求數(shù)據(jù),并通過方法中回調(diào)函數(shù)的參數(shù)返回請(qǐng)求的數(shù)據(jù),它的調(diào)用格式如下:
$.get(url,[callback])
參數(shù)url為服務(wù)器請(qǐng)求地址,可選項(xiàng)callback參數(shù)為請(qǐng)求成功后執(zhí)行的回調(diào)函數(shù)。
例如,當(dāng)點(diǎn)擊“加載”按鈕時(shí),調(diào)用get()方法向服務(wù)器中的一個(gè).php文件以GET方式請(qǐng)求數(shù)據(jù),并將返回的數(shù)據(jù)內(nèi)容顯示在頁(yè)面中,
?
<body><div id="divtest"><div class="title"><span class="fl">我的個(gè)人資料</span> <span class="fr"><input id="btnShow" type="button" value="加載" /></span></div><ul></ul></div><script type="text/javascript">$(function () {$("#btnShow").bind("click", function () {var $this = $(this);$.get("http://www.imooc.com/data/info_f.php",function(data){$this.attr("disabled", "true");$("ul").append("<li>我的名字叫:" + data.name + "</li>");$("ul").append("<li>男朋友對(duì)我說:" + data.say + "</li>");}, "json");})});</script></body>
5、使用post()方法以post方式向服務(wù)器發(fā)送數(shù)據(jù)
?
與get()方法相比,post()方法多用于以POST方式向服務(wù)器發(fā)送數(shù)據(jù),服務(wù)器接收到數(shù)據(jù)之后,進(jìn)行處理,并將處理結(jié)果返回頁(yè)面,調(diào)用格式如下:
$.post(url,[data],[callback])
參數(shù)url為服務(wù)器請(qǐng)求地址,可選項(xiàng)data為向服務(wù)器請(qǐng)求時(shí)發(fā)送的數(shù)據(jù),可選項(xiàng)callback參數(shù)為請(qǐng)求成功后執(zhí)行的回調(diào)函數(shù)。
例如,在輸入框中錄入一個(gè)數(shù)字,點(diǎn)擊“檢測(cè)”按鈕,調(diào)用post()方法向服務(wù)器以POST方式發(fā)送請(qǐng)求,檢測(cè)輸入值的奇偶性,并顯示在頁(yè)面中,
?
<body><div id="divtest"><div class="title"><span class="fl">檢測(cè)數(shù)字是否大于0</span> <span class="fr"><input id="btnCheck" type="button" value="檢測(cè)" /></span></div><ul><li>請(qǐng)求輸入一個(gè)數(shù)字 <input id="txtNumber" type="text" size="12" /></li></ul></div><script type="text/javascript">$(function () {$("#btnCheck").bind("click", function () {$.post('http://www.imooc.com/data/check_f.php',{num:$("#txtNumber").val()},function (data) {$("ul").append("<li>你輸入的<b> "+ $("#txtNumber").val() + " </b>是<b> "+ data + " </b></li>");});})});</script></body>
6、使用serialize()方法序列化表單元素值
?
使用serialize()方法可以將表單中有name屬性的元素值進(jìn)行序列化,生成標(biāo)準(zhǔn)URL編碼文本字符串,直接可用于ajax請(qǐng)求,它的調(diào)用格式如下:
$(selector).serialize()
其中selector參數(shù)是一個(gè)或多個(gè)表單中的元素或表單元素本身。
例如,在表單中添加多個(gè)元素,點(diǎn)擊“序列化”按鈕后,調(diào)用serialize()方法,將表單元素序列化后的標(biāo)準(zhǔn)URL編碼文本字符串顯示在頁(yè)面中,
?
?
?
- Text1=sd&Select1=0&Checkbox1=on
從圖中可以看出,當(dāng)點(diǎn)擊“序列化”按鈕后,調(diào)用表單元素本身的serialize()方法,將表單中元素全部序列化,生成標(biāo)準(zhǔn)URL編碼,各元素間通過&號(hào)相聯(lián)。
?
?
7、使用ajax(0方法加載服務(wù)器數(shù)據(jù)
使用ajax()方法是最底層、功能最強(qiáng)大的請(qǐng)求服務(wù)器數(shù)據(jù)的方法,它不僅可以獲取服務(wù)器返回的數(shù)據(jù),還能向服務(wù)器發(fā)送請(qǐng)求并傳遞數(shù)值,它的調(diào)用格式如下:
jQuery.ajax([settings])或$.ajax([settings])
其中參數(shù)settings為發(fā)送ajax請(qǐng)求時(shí)的配置對(duì)象,在該對(duì)象中,url表示服務(wù)器請(qǐng)求的路徑,data為請(qǐng)求時(shí)傳遞的數(shù)據(jù),dataType為服務(wù)器返回的數(shù)據(jù)類型,success為請(qǐng)求成功的執(zhí)行的回調(diào)函數(shù),type為發(fā)送數(shù)據(jù)請(qǐng)求的方式,默認(rèn)為get。
例如,點(diǎn)擊頁(yè)面中的“加載”按鈕,調(diào)用ajax()方法向服務(wù)器請(qǐng)求加載一個(gè)txt文件,并將返回的文件中的內(nèi)容顯示在頁(yè)面,
?
<body><div id="divtest"><div class="title"><span class="fl">檢測(cè)數(shù)字的奇偶性</span> <span class="fr"><input id="btnCheck" type="button" value="檢測(cè)" /></span></div><ul><li>請(qǐng)求輸入一個(gè)數(shù)字 <input id="txtNumber" type="text" size="12" /></li></ul></div><script type="text/javascript">$(function () {$("#btnCheck").bind("click", function () {$.ajax({url:"http://www.imooc.com/data/check.php",type:"post",dataType:'text',data: {num: $("#txtNumber").val() },success: function (data) {$("ul").append("<li>你輸入的<b> "+ $("#txtNumber").val() + " </b>是<b> "+ data + " </b></li>");}});})});</script></body>?
?
8、使用ajaxSetup()方法設(shè)置全局Ajax默認(rèn)選項(xiàng)值
?
?
使用ajaxSetup()方法可以設(shè)置Ajax請(qǐng)求的一些全局性選項(xiàng)值,設(shè)置完成后,后面的Ajax請(qǐng)求將不需要再添加這些選項(xiàng)值,它的調(diào)用格式為:
jQuery.ajaxSetup([options])或$.ajaxSetup([options])
可選項(xiàng)options參數(shù)為一個(gè)對(duì)象,通過該對(duì)象設(shè)置Ajax請(qǐng)求時(shí)的全局選項(xiàng)值。
例如,先調(diào)用ajaxSetup()方法設(shè)置全局的Ajax選項(xiàng)值,再點(diǎn)擊兩個(gè)按鈕,分別使用ajax()方法請(qǐng)求不同的服務(wù)器數(shù)據(jù),并將數(shù)據(jù)內(nèi)容顯示在頁(yè)面,
?
?
?
<body><div id="divtest"><div class="title"><span class="fl">奇偶性和是否大于0</span> <span class="fr"><input id="btnShow_1" type="button" value="驗(yàn)證1" /><input id="btnShow_2" type="button" value="驗(yàn)證2" /></span></div><ul><li>請(qǐng)求輸入一個(gè)數(shù)字 <input id="txtNumber" type="text" size="12" /></li></ul></div><script type="text/javascript">$(function () {$.ajaxSetup({dataType:"text",type:"post",success:function(data){$("ul").append("<li>你輸入的<b> "+ $("#txtNumber").val() + " </b>是<b> "+ data + " </b></li>");}});$("#btnShow_1").bind("click", function () {$.ajax({data: { num: $("#txtNumber").val() },url: "http://www.imooc.com/data/check.php"});})$("#btnShow_2").bind("click", function () {$.ajax({data: { num: $("#txtNumber").val() },url: "http://www.imooc.com/data/check_f.php"});})});</script></body>
9、使用ajaxStart()和ajaxStop()方法
?
ajaxStart()和ajaxStop()方法是綁定Ajax事件。ajaxStart()方法用于在Ajax請(qǐng)求發(fā)出前觸發(fā)函數(shù),ajaxStop()方法用于在Ajax請(qǐng)求完成后觸發(fā)函數(shù)。它們的調(diào)用格式為:
$(selector).ajaxStart(function())和$(selector).ajaxStop(function())
其中,兩個(gè)方法中括號(hào)都是綁定的函數(shù),當(dāng)發(fā)送Ajax請(qǐng)求前執(zhí)行ajaxStart()方法綁定的函數(shù),請(qǐng)求成功后,執(zhí)行ajaxStop ()方法綁定的函數(shù)。
例如,在調(diào)用ajax()方法請(qǐng)求服務(wù)器數(shù)據(jù)前,使用動(dòng)畫顯示正在加載中,當(dāng)請(qǐng)求成功后,該動(dòng)畫自動(dòng)隱藏,
?
<body><div id="divtest"><div class="title"><span class="fl">加載一段文字</span> <span class="fr"><input id="btnShow" type="button" value="加載" /></span></div><ul><li id="divload"></li></ul></div><script type="text/javascript">$(function () {$(document).ajaxStart(function(){$("#divload").html("正在請(qǐng)求數(shù)據(jù)...");});$(document).ajaxStop(function(){$("#divload").html("數(shù)據(jù)請(qǐng)求完成!");$("#divload").attr("hidden","true");});$("#btnShow").bind("click", function () {var $this = $(this);$.ajax({url: "http://www.imooc.com/data/info_f.php",dataType: "json",success: function (data) {$this.attr("disabled", "true");$("ul").append("<li>我的名字叫:" + data.name + "</li>");$("ul").append("<li>男朋友對(duì)我說:" + data.say + "</li>");}});})});</script></body>?
最近在整理一些資源工具,放在網(wǎng)站分享?http://tools.maqway.com
歡迎關(guān)注公眾號(hào):麻雀唯伊 , 不定時(shí)更新資源文章,生活優(yōu)惠,或許有你想看的
?
?
?
總結(jié)
以上是生活随笔為你收集整理的JQuery之Ajax方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利益交错-HTML5视频标准之争
- 下一篇: riak教程 java_Riak学习(2