亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

jQuery ajax錯誤函數

jQuery ajax錯誤函數

慕田峪7331174 2019-08-09 17:19:54
jQuery ajax錯誤函數我有一個ajax調用將數據傳遞給頁面,然后返回一個值。我已經從頁面檢索了成功的調用,但我編寫了它,以便在asp中引發錯誤。我如何從jquery中檢索該錯誤?例如:cache: false,url: "addInterview_Code.asp",type: "POST",datatype: "text",data: strData,success: function (html) {     alert('successful : ' + html);     $("#result").html("Successful");},error: function (error) {     **alert('error; ' + eval(error));**}這是我不理解的錯誤位。在函數中我需要放置什么參數,以便我可以使用我在服務器中引發的錯誤消息。
查看完整描述

3 回答

?
吃雞游戲

TA貢獻1829條經驗 獲得超7個贊

Ajax error函數中的必需參數是jqXHR, exception,您可以像下面這樣使用它:

$.ajax({
    url: 'some_unknown_page.html',
    success: function (response) {
        $('#post').html(response.responseText);
    },
    error: function (jqXHR, exception) {
        var msg = '';
        if (jqXHR.status === 0) {
            msg = 'Not connect.\n Verify Network.';
        } else if (jqXHR.status == 404) {
            msg = 'Requested page not found. [404]';
        } else if (jqXHR.status == 500) {
            msg = 'Internal Server Error [500].';
        } else if (exception === 'parsererror') {
            msg = 'Requested JSON parse failed.';
        } else if (exception === 'timeout') {
            msg = 'Time out error.';
        } else if (exception === 'abort') {
            msg = 'Ajax request aborted.';
        } else {
            msg = 'Uncaught Error.\n' + jqXHR.responseText;
        }
        $('#post').html(msg);
    },});

DEMO FIDDLE


參數

jqXHR:

它實際上是一個看起來像這樣的錯誤對象

您還可以在自己的瀏覽器控制臺中查看此內容,方法是使用console.log以下error函數:

error: function (jqXHR, exception) {
    console.log(jqXHR);
    // Your error handling logic here..}

我們使用status此對象的屬性來獲取錯誤代碼,就像我們獲得status = 404一樣,這意味著找不到請求的頁面。它根本不存在?;谠摖顟B代碼,我們可以將用戶重定向到登錄頁面或我們的業務邏輯需要的任何內容。

例外:

這是字符串變量,顯示異常類型。因此,如果我們收到404錯誤,則exception文本只是“錯誤”。同樣,我們可能會將'超時','中止'作為其他異常文本。


取消通知:jqXHR.success(),jqXHR.error()jqXHR.complete()回調棄用的jQuery 1.8。要準備的代碼為他們的最終消除,使用jqXHR.done(),jqXHR.fail()jqXHR.always()來代替。

因此,如果您使用的是jQuery 1.8或更高版本,我們需要更新成功和錯誤函數邏輯,如: -

// Assign handlers immediately after making the request,// and remember the jqXHR object for this requestvar jqxhr = $.ajax("some_unknown_page.html")
    .done(function (response) {
        // success logic here
        $('#post').html(response.responseText);
    })
    .fail(function (jqXHR, exception) {
        // Our error logic here
        var msg = '';
        if (jqXHR.status === 0) {
            msg = 'Not connect.\n Verify Network.';
        } else if (jqXHR.status == 404) {
            msg = 'Requested page not found. [404]';
        } else if (jqXHR.status == 500) {
            msg = 'Internal Server Error [500].';
        } else if (exception === 'parsererror') {
            msg = 'Requested JSON parse failed.';
        } else if (exception === 'timeout') {
            msg = 'Time out error.';
        } else if (exception === 'abort') {
            msg = 'Ajax request aborted.';
        } else {
            msg = 'Uncaught Error.\n' + jqXHR.responseText;
        }
        $('#post').html(msg);
    })
    .always(function () {
        alert("complete");
    });

希望能幫助到你!


查看完整回答
反對 回復 2019-08-09
?
慕桂英546537

TA貢獻1848條經驗 獲得超10個贊

試試這個:

error: function(jqXHR, textStatus, errorThrown) {
  console.log(textStatus, errorThrown);}

如果您想通知您的前端有關驗證錯誤,請嘗試返回json:

dataType: 'json',success: function(data, textStatus, jqXHR) {
   console.log(data.error);}

你的asp腳本應該返回:

{"error": true}


查看完整回答
反對 回復 2019-08-09
?
郎朗坤

TA貢獻1921條經驗 獲得超9個贊

這是你如何拉出asp錯誤。

              cache: false,
              url: "addInterview_Code.asp",
              type: "POST",
              datatype: "text",
              data: strData,
              success: function (html) {
                  alert('successful : ' + html);
                  $("#result").html("Successful");
              },
              error: function (jqXHR, textStatus, errorThrown) {
                  if (jqXHR.status == 500) {
                      alert('Internal error: ' + jqXHR.responseText);
                  } else {
                      alert('Unexpected error.');
                  }
              }


查看完整回答
反對 回復 2019-08-09
  • 3 回答
  • 0 關注
  • 662 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號