jQuery ajax 的用法
url:要呼叫的 url讀取資料時會有loading訊息要增加以下兩個參數
data:要傳的值,可以用 {a:"abc", b:"def"}
type:"POST",也可以用GET
dataType:"text",回傳的格式,如回傳是json 就為json
success:成功
error:失敗或錯誤
complete:請求完成時執行的函式(不論結果是success或error)。
beforeSend:發送請求之前會執行的函式。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.ajax({ | |
url: URLs, | |
data: $('#sentToBack').serialize(), | |
type:"POST", | |
dataType:'text', | |
success: function(msg){ | |
alert(msg); | |
}, | |
beforeSend:function(){ | |
$('#loadingIMG').show(); | |
}, | |
complete:function(){ | |
$('#loadingIMG').hide(); | |
}, | |
error:function(xhr, ajaxOptions, thrownError){ | |
alert(xhr.status); | |
alert(thrownError); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form id="sentToBack"> | |
<input type="text" name="Text"/> | |
<input type="button" value="送出" onClick="Submit()"/> | |
</form> | |
<div id="loadingIMG" style="display:none"><img src="loading.gif" height='14'/>資料處理中,請稍後。</div> |
他會去執行Submit()的函數。其後執行ajax的運作。
當他開始運作的時候,會先去執行beforeSend裡面的function,
當他開始運作的時候,會先去執行beforeSend裡面的function,
這時會把loadingIMG顯示出來($('#loadingIMG').show();),
當執行完成之後,不論是成功或是有錯誤,
都會去執行complete,在去把loadingIMG隱藏起來
($('#loadingIMG').hide();)。
Loading 圖示可以從以下網站取得
http://ajaxload.info/