jquery第二十四課,ajax事件處理,也就有常見的幾種,表示在ajax處理之后,通過結果事件來調用不同的函數,比如,ajax執行完畢,ajax出現錯誤等等.
1.ajaxcomplete(callback)ajax 請求完成時執行函數。ajax 事件。
callback是回調函數,當對象的ajax執行完畢后,調用該函數,函數可以有參數function(event,request, settings){}
event是事件名稱 為ajaxcomplete
request是xmlhttprequest對象
settings表示對象提交設置,比如settings.type,如果是get方式提交,返回get,如果是post方式提交返回post,比如settings.url是提交調用的頁面以及參數
舉例:
$(#cn).bind(click,function(){$.get(index4.php,{foraspcnurl:$(#forasp).val()},function(msg){alert(msg);} );});
$(#cn).ajaxcomplete(function(event,request, settings){alert(settings.type);});
2.ajaxerror(callback),ajax 請求發生錯誤時執行函數
callback是返回函數.function(event,xmlhttprequest,ajaxoptions,thrownerror) {}在處理函數中可以用this表示監聽的dom元素
參數:
event是觸發的事件當前event.type表示ajaxerror事件.
xmlhttprequest返回錯誤信息內容,xmlhttprequest.responsetext就是text文本形式輸出的內容.
ajaxoptions跟上面的settings相似,是一些參數的調用:ajaxoptions.url,則返回調用頁面以及參數
thrownerror只有當異常發生時才會被傳遞,這個不是很理解.
舉例:
$(#cn).bind(click,function(){$.get(index100.php,{foraspcnurl:$(#forasp).val()},function(msg){alert(msg);} );});
$(#cn).ajaxerror(function(event,xmlhttprequest,ajaxoptions,thrownerror){alert(ajaxoptions.url);});});
3.ajaxsend(callback)ajax 請求發送前執行函數。
callback 回調函數:function(evt,request,settings){});
參數evt 是 是事件,request是xmlhttprequest對象,setting是提交的設置.更ajaxcomplete類似.
4.ajaxstart(callback) ajax請求開始時執行函數
5.ajaxstop(callback)ajax請求結束時執行函數
6.ajaxsuccess(callback)ajax請求成功時執行函數。
4,5,6中的callback是執行時調用的函數,function(){},沒有參數.直接在函數體內寫執行.