日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

ajax请求成功,失败处理!

發布時間:2024/1/18 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ajax请求成功,失败处理! 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • 請求失敗處理

  • function requesFail(xhr){var status = xhr.status;if (status) {showNotify("error", "網絡錯誤", "發生網絡錯誤,錯誤碼為:" + xhr.status);} else {showNotify("error", "網絡錯誤", "未知網絡錯誤, 請確保設備處在聯網狀態");} }

    請求成功處理

    function requestOk(data,errMsg,callbackOk,callbackError){if(data==undefined){showNotify('info','提示','無法從服務器獲取詳細數據,請聯系管理員')}else if(data.code==1){callbackOk(data);}else{callbackError(data,errMsg);} }

    *請求成功,返回data:{code:0,data:xx},此處認為code=0,為成功,調用成功后執行

    callbackOk 是該請求成功后要進行的操作

    code!=0即為出錯,執行出錯方法:

    實例:

    $('#toPost').click(function(){console.log('Post');$.ajax({type:"Post",url:postUrl,data:{account:'cd',password:'121212'},dataType:'json',success:function(data){requestOk(data,'',function(){console.log(data);},customCodeError);},error:function(xhr,state,errorThrown){requesFail(xhr);}});});

    若code!=0 ,沒有特殊的code=n,需要做特別處理,則調用customCodeError,若有特殊處理,則根據特殊情況,自行封裝。

    如:

    function postCodeError(data,errMsg){let code=data.code;if(code==1001){showNotify("info", "錯誤", "不合法用戶,錯誤碼:"+code);}else{showNotify("info", "錯誤", "用戶不存在,錯誤碼:"+code);} }

    showNotify(),借助pnotify插件的一種顯示錯誤消息的面板。可根據自己的需求進行封裝。應用時需導入文件

    < link? href= "static/css/pnotify.css"? rel= "stylesheet" />
    < script? src= "static/js/common/pnotify.js" > < / script >


    /* notify 配置* 使用 showNotify("info", "提示", "這是提示語句"); showNotify("success", "成功啦", "這是成功語句"); showNotify("error", "失敗", "這是失敗語句"); *****************************************************************/ function showNotify(type, title, text) {var icons;if (type == "success") {icons = "fa fa-check-circle";}if (type == "error") {icons = "fa fa-exclamation-triangle";}if (type == "info") {icons = "fa fa-exclamation-circle";}var notice = new PNotify({title: title,text: text,type: type,delay: 2000,icon: icons,addclass: "notifyOnly",styling: "fontawesome",buttons: {closer: true,sticker: true}});notice.get().click(function() {notice.remove();}); }

    總結

    以上是生活随笔為你收集整理的ajax请求成功,失败处理!的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。