国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

jquery $.ajax相關用法分享

2024-05-06 14:22:19
字體:
來源:轉載
供稿:網友
代碼如下:
$.ajax({
type: "GET",
url: "Services/EFService.svc/Members",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
// Play with returned data in JSON format
},
error: function (msg) {
alert(msg);
}
});

全局事件是每次的Ajax請求都會觸發的,它會向DOM中的所有元素廣播,在上面 getScript() 示例中加載的腳本就是全局Ajax事件。全局事件可以如下定義:
代碼如下:
$("#loading").ajaxStart(function(){
$(this).show();
});

我們可以在特定的請求將全局事件禁用,只要設置下 global 選項就可以了:
代碼如下:
$.ajax({
url: "test.html",
global: false,// 禁用全局Ajax事件. // ... });

下面是jQuery官方給出的完整的Ajax事件列表:
•ajaxStart (Global Event)
This event is broadcast if an Ajax request is started and no other Ajax requests are currently running.
•beforeSend (Local Event)
This event, which is triggered before an Ajax request is started, allows you to modify the XMLHttpRequest object (setting additional headers, if need be.)
•ajaxSend (Global Event)
This global event is also triggered before the request is run.
•success (Local Event)
This event is only called if the request was successful (no errors from the server, no errors with the data).
•ajaxSuccess (Global Event)
This event is also only called if the request was successful.
•error (Local Event)
This event is only called if an error occurred with the request (you can never have both an error and a success callback with a request).
•ajaxError (Global Event)
This global event behaves the same as the local error event.
•complete (Local Event)
This event is called regardless of if the request was successful, or not. You will always receive a complete callback, even for synchronous requests.
•ajaxComplete (Global Event)
This event behaves the same as the complete event and will be triggered every time an Ajax request finishes.
•ajaxStop (Global Event)
This global event is triggered if there are no more Ajax requests being processed.
jQuery.ajaxSetup( options ) : 設置全局 AJAX 默認選項。
設置 AJAX 請求默認地址為 "/xmlhttp/",禁止觸發全局 AJAX 事件,用 POST 代替默認 GET 方法。其后的 AJAX 請求不再設置任何選項參數。
jQuery 代碼:
代碼如下:
$.ajaxSetup({
url: "/xmlhttp/",
global: false,
type: "POST"
});
$.ajax({ data: myData });


如果指定為 html 類型,任何內嵌的 JavaScript 都會在 HTML 作為一個字符串返回之前執行。類似地,指定 script 類型的話,也會先執行服務器端生成 JavaScript,然后再把腳本作為一個文本數據返回。
JSON 數據是一種能很方便通過 JavaScript 解析的結構化數據。如果獲取的數據文件存放在遠程服務器上(域名不同,也就是跨域獲取數據),則需要使用 jsonp 類型。使用這種類型的話,會創建一個查詢字符串參數 callback=? ,這個參數會加在請求的 URL 后面。服務器端應當在 JSON 數據前加上回調函數名,以便完成一個有效的 JSONP 請求。如果要指定回調函數的參數名來取代默認的 callback,可以通過設置 $.ajax() 的 jsonp 參數。   
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 竹山县| 溆浦县| 合江县| 洛宁县| 隆林| 砚山县| 梁山县| 夏河县| 赣榆县| 大姚县| 安康市| 潼关县| 同心县| 平乡县| 张家川| 三原县| 松阳县| 鸡东县| 舟山市| 山阳县| 甘德县| 东乡县| 正宁县| 浦北县| 玉山县| 嘉鱼县| 云龙县| 东莞市| 西藏| 手机| 盐城市| 紫云| 余庆县| 天峨县| 江西省| 聊城市| 纳雍县| 高雄市| 常山县| 区。| 清涧县|