jquery之ajax請求工具類
js中調用方式:
var params = {};params["aakey"]="aavalue";params["bbkey"]="bbvalue";$.cosajax({ url: path + "**.action", data: params, //一個對象,包含很多索引值對,jquery自動轉化為aakey=aavalue&bbkey=bbvalue的形式 callback: function(result) {}});
function checkSessionTimeOut(result) { //增加session逾時判斷 if (result &&!(result.success) && result.message == 'timeout') { top.document.location= path + '/frame.action'; returntrue; } return false;}function showTip(msg,callback) { $("#mask_bg").css("z-index","9999"); //視窗置於最上面 $("#okbtn").unbind(); //刪除okbtn的其他事件 $("#mask_new").show(); $("#okbtn").one('click', callback); //增加點擊事件,隱藏tip } function hideTip() { $("#mask_new").hide(); $("#mask_bg").hide(); }jQuery.extend({ cosajax : function(opts) { opts= jQuery.extend({ url : "", data : "", isWait: false, isClose: true, callback : function() { returnfalse; }, fail : function() { returnfalse; } },opts || {}); //逗號後面opts||{}是對前面的擴充 $.ajax({ type : "POST", dataType : "json", url : opts.url, async : false, data : opts.data, //多個參數用&串連 timeout: 180000, beforeSend : function() { }, complete : function(result) { if (!opts.isWait) { //成功失敗都會調用complete方法 hiddenWait(); } }, success : function(result) { // 增加session逾時判斷 if(checkSessionTimeOut(result)) { return; } if (result.success) { opts.callback(result); //調用opts中callback方法 }else{ if(opts.isClose) { //關閉視窗 closeWindows(); var errTipMessage =result.message; switch (result.message) { case'500': errTipMessage=”操作失敗”; break; default: errTipMessage= result.message; break; } showTip(errTipMessage,hideTip); //hiteTip是點擊事件的回呼函數 } else { hiddenWait(); opts.fail(result); } } }, error : function() { if(opts.isClose) { closeWindows(); showTip(“操作失敗”,hideTip); }else{ hiddenWait(); opts.fail(); } } }); }});