日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

jQuery -- 光阴似箭(五):AJAX 方法

發布時間:2023/12/2 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jQuery -- 光阴似箭(五):AJAX 方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

jQuery?-- 知識點回顧篇(五):AJAX 方法

1. $.ajax 方法:用于執行 AJAX(異步 HTTP)請求。

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>My Test JQuery</title><script type="text/javascript" src="./js/jquery-1.10.2.min.js" ></script><script type="text/javascript" > $(function(){ //語法格式:$.ajax({name:value, name:value, ... })//該參數規定 AJAX 請求的一個或多個名稱/值對。 $("#btn_ajax").click(function(){$.ajax({url:"Test_ajax.aspx",success:function(result){$("#myDiv1").html(result);}});});});</script> </head> <body><button type="button" id="btn_ajax">ajax</button><div id="myDiv1" style="width:210px;height:30px;padding: 10px;border:2px solid blue;"></div> </body> </html>

?

2. $.ajaxSetup() 方法:為將來的 AJAX 請求設置默認值。

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>My Test JQuery</title><script type="text/javascript" src="./js/jquery-1.10.2.min.js"></script><script type="text/javascript" > $(function(){ //語法格式:$.ajaxSetup({name:value, name:value, ... })//該參數為帶有一個或多個名稱/值對的 AJAX 請求規定設置。 $("#btn_ajax").click(function(){$.ajaxSetup({url:"Test_ajax.aspx",success:function(result){$("#myDiv1").html(result);}});$.ajax();});});</script> </head> <body><button type="button" id="btn_ajax">ajax</button><div id="myDiv1" style="width:210px;height:30px;padding: 10px;border:2px solid blue;"></div> </body> </html>

?

3. $.get() 方法:使用 HTTP GET 請求從服務器加載數據。

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>My Test JQuery</title><script type="text/javascript" src="./js/jquery-1.10.2.min.js" ></script><script type="text/javascript" > $(function(){ //語法格式:$.get(URL,data,function(data,status,xhr),dataType)//URL: 必需參數。規定您需要請求的 URL。//data: 可選參數。規定連同請求發送到服務器的數據。//function(data,status,xhr):可選參數。規定當請求成功時運行的函數。//dataType:可選參數。規定預期的服務器響應的數據類型。 $("#btn_ajax").click(function(){$.get("Test_ajax.aspx",function(data){alert("數據: " data );});});});</script> </head> <body><button type="button" id="btn_ajax">ajax</button><div id="myDiv1" style="width:210px;height:30px;padding: 10px;border:2px solid blue;"></div> </body> </html>

?

4. $.getJSON() 方法:使用 AJAX 的 HTTP GET 請求獲取 JSON 數據。

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>My Test JQuery</title><script type="text/javascript" src="./js/jquery-1.10.2.min.js"></script><script type="text/javascript" > $(function(){$("#btn_ajax").click(function(){//語法格式: $(selector).getJSON(url,data,success(data,status,xhr))//url:必需參數。規定將請求發送到哪個 URL。//data:可選參數。規定發送到服務器的數據。//success(data,status,xhr):可選參數。規定當請求成功時運行的函數。 $.getJSON("Test_ajax.aspx",function(result){$("myDiv1").text(result);});});});</script> </head> <body><button type="button" id="btn_ajax">ajax</button><div id="myDiv1" style="width:210px;height:30px;padding: 10px;border:2px solid blue;"></div> </body> </html>

?

5. $.getScript() 方法:使用 AJAX 的 HTTP GET 請求獲取和執行 JavaScript。

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>My Test JQuery</title><script type="text/javascript" src="./js/jquery-1.10.2.min.js"></script><script type="text/javascript" > $(function(){$("#btn_ajax").click(function(){//語法格式: $(selector).getScript(url,success(response,status))//url: 必需參數。規定將請求發送到哪個 URL。//success(response,status): 可選參數。規定當請求成功時運行的函數。 $.getScript("Test_ajax_script.js");});});</script> </head> <body><button type="button" id="btn_ajax">ajax</button><div id="myDiv1" style="width:210px;height:30px;padding: 10px;border:2px solid blue;"></div> </body> </html>

?

6. $.param() 方法創建數組或對象的序列化表示形式。序列化的值可在生成 AJAX 請求時用于 URL 查詢字符串中。

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>My Test JQuery</title><script type="text/javascript" src="./js/jquery-1.10.2.min.js"></script><script type="text/javascript" > $(function(){personObj=new Object();personObj.name="xiaohuzi";personObj.age=26;personObj.web="xiaohuzi.test.com"; //語法格式:$.param(object,trad)//object: 必需參數。規定要序列化的數組或對象。//trad: 可選參數。布爾值,指定是否使用參數序列化的傳統樣式。 $("#btn_ajax").click(function(){$("#myDiv1").text($.param(personObj));});});</script> </head> <body><button type="button" id="btn_ajax">ajax</button><div id="myDiv1" style="height:30px;padding: 10px;border:2px solid blue;"></div> </body> </html>

?

7. $.post() 方法:使用 HTTP POST 請求從服務器加載數據。

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>My Test JQuery</title><script type="text/javascript" src="./js/jquery-1.10.2.min.js"></script><script type="text/javascript" > $(function(){//語法格式:$(selector).post(URL,data,function(data,status,xhr),dataType)//URL: 必需參數。規定將請求發送到哪個 URL。//data: 可選參數。規定連同請求發送到服務器的數據。//function(data,status,xhr): 可選參數。規定當請求成功時運行的函數。//dataType: 可選參數。規定預期的服務器響應的數據類型。 $("#btn_ajax").click(function(){$.post("Test_ajax.aspx",function(data){alert("Data: " data );});});});</script> </head> <body><button type="button" id="btn_ajax">ajax</button><div id="myDiv1" style="height:30px;padding: 10px;border:2px solid blue;"></div> </body> </html>

?

8. ajaxComplete() 方法:規定 AJAX 請求完成時運行的函數。

 ajaxStart() 方法:規定 AJAX 請求開始時運行的函數。

?? ajaxSend() 方法:規定 AJAX 請求即將發送時運行的函數。

?? ajaxError() 方法:規定 AJAX 請求失敗時運行的函數。

?? ajaxStop() 方法:規定所有的 AJAX 請求完成時運行的函數。

?? ajaxSuccess() 方法:規定 AJAX 請求成功完成時運行的函數。

?? load() 方法:從服務器加載數據,并把返回的數據放置到指定的元素中。

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>My Test JQuery</title><script type="text/javascript" src="./js/jquery-1.10.2.min.js"></script><script type="text/javascript" > $(function(){//語法格式:$(document).ajaxSend(function(event,xhr,options))//function(event,xhr,options) 必需。規定當請求成功時運行的函數。 $(document).ajaxSend(function(){$("#myDiv1").css("border","5px solid pink");});//語法格式:$(document).ajaxStart(function())//function(): 必需參數。規定當 AJAX 請求開始時運行的函數。 $(document).ajaxStart(function(){$("#myDiv1").css("display","block");});//語法格式:$(document).ajaxStop(function())//function(): 必需參數。規定所有的 AJAX 請求完成時運行的函數。 $(document).ajaxStop(function(){$("#myDiv1").css("border","3px solid green");});//語法格式:$(document).ajaxError(function(event,xhr,options,exc))//function(event,xhr,options,exc):必需參數。規定當請求失敗時運行的函數。 $(document).ajaxError(function(){$("#myDiv1").css("border","3px solid red");});//語法格式:$(document).ajaxSuccess(function(event,xhr,options))//function(event,xhr,options): 必需參數。規定如果請求成功時運行的函數。 $(document).ajaxSuccess(function(){$("#myDiv1").css("border","3px solid yellow");});//語法格式:$(document).ajaxComplete(function(event,xhr,options))//function(event,xhr,options): 必需參數。規定當請求完成時運行的函數。 $(document).ajaxComplete(function(){$("#myDiv1").css("display","none");});//語法格式:$(selector).load(url,data,function(response,status,xhr))//url:必需參數。規定您需要加載的 URL。//data:可選參數。規定連同請求發送到服務器的數據。//function(response,status,xhr):可選參數。規定 load() 方法完成時運行的回調函數。 $("#btn_ajax").click(function(){$("#txt").load("Test_ajax.aspx");});});</script> </head> <body><button type="button" id="btn_ajax">ajax</button><div id="myDiv1" style="height:30px;padding: 10px;border:2px solid blue;"></div> </body> </html>

?

9. serialize() 方法:通過序列化表單值創建 URL 編碼文本字符串。

??? serializeArray() 方法:通過序列化表單值來創建對象(name 和 value)的數組。

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>My Test JQuery</title><script type="text/javascript" src="./js/jquery-1.10.2.min.js"></script><script type="text/javascript" > $(function(){//語法格式:$(selector).serialize()//您可以選擇一個或多個表單元素(如輸入和/或文本區),或表單元素本身。//序列化的值可在生成 AJAX 請求時用于 URL 查詢字符串中。 $("#btn_serialize").click(function(){$("#myDiv1").text($("form").serialize());});//語法格式:$(selector).serializeArray()//serializeArray() 方法通過序列化表單值來創建對象(name 和 value)的數組。//您可以選擇一個或多個表單元素(如輸入和/或文本區),或表單元素本身。 $("#btn_serializeArray").click(function(){x=$("form").serializeArray();$.each(x, function(i, field){$("#myDiv2").append(field.name ":" field.value " ");});});});</script> </head> <body><form action="">姓名: <input type="text" name="Name" value="XiaoHuzi" /><br>年齡: <input type="text" name="Age" value="26" /><br>工作: <input type="text" name="Job" value="IT" /><br></form><button type="button" id="btn_serialize">serialize</button><button type="button" id="btn_serializeArray">serializeArray</button><div id="myDiv1" style="height:30px;padding: 10px;border:2px solid blue;"></div> <div id="myDiv2" style="height:30px;padding: 10px;border:2px solid green;"></div> </body> </html>

  

?


更多專業前端知識,請上 【猿2048】www.mk2048.com

總結

以上是生活随笔為你收集整理的jQuery -- 光阴似箭(五):AJAX 方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。