jquery之ajax請求工具類

來源:互聯網
上載者:User

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();                }            }        });   }});

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.