복붙노트

[JQUERY] jQuery를 AJAX 오차 함수

JQUERY

jQuery를 AJAX 오차 함수

해결법


  1. 1.아약스 오류 함수의 필수 매개 변수는 jqXHR, 예외 및 아래처럼 사용할 수 있습니다 :

    아약스 오류 함수의 필수 매개 변수는 jqXHR, 예외 및 아래처럼 사용할 수 있습니다 :

    $.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 뿐인

    jqXHR :

    이처럼 보이는 그 실제로 오류 개체

    또한 오차 함수와 같은 내부 CONSOLE.LOG을 사용하여, 자신 만의 브라우저 콘솔에서이를 볼 수 있습니다

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

    우리가 요청한 페이지를 찾을 수 없음이 수단 상태 = (404)을받을 경우처럼 우리는, 에러 코드를 얻기 위해이 객체에서 상태 속성을 사용하고 있습니다. 그것은 모든 존재하지 않습니다. 이 상태 코드를 바탕으로 우리는 로그인 페이지 또는 무엇이든 우리의 비즈니스 로직을 필요로 사용자를 리디렉션 할 수 있습니다.

    예외:

    이 예외의 종류를 표시 문자열 변수이다. 우리가 404 오류가 발생하는 경우에 따라서, 예외 텍스트는 단순히 '오류'가 될 것입니다. 마찬가지로, 우리는 다른 예외 텍스트로 '중단', '타임 아웃'을 얻을 수 있습니다.

    그래서, 경우에 당신은 jQuery를 1.8를 사용하고 또는 우리가 같이 성공 및 오류 기능 로직을 업데이트해야합니다 이상 -

    // Assign handlers immediately after making the request,
    // and remember the jqXHR object for this request
    var 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");
        });
    

    희망이 도움이!


  2. 2.이 시도:

    이 시도:

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

    당신이 유효성 검사 오류에 대해 프론트 엔드를 통보 할 경우, JSON을 반환하려고 :

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

    귀하의 ASP 스크립트 반환 schould :

    {"error": true}
    

  3. 3.여기에 당신이 ASP 오류 밖으로 당겨 방법이다.

    여기에 당신이 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.');
                      }
                  }
    

  4. 4.

    error(jqXHR, textStatus, errorThrown)
    

    http://api.jquery.com/jQuery.ajax/


  5. 5.

              cache: false,
              url: "addInterview_Code.asp",
              type: "POST",
              datatype: "text",
              data: strData,
              success: function (html) {
                  alert('successful : ' + html);
                  $("#result").html("Successful");
              },
              error: function(data, errorThrown)
              {
                  alert('request failed :'+errorThrown);
              }
    

  6. 6.당신은 기능을 사용하고

    당신은 기능을 사용하고

    error(error) 
    

    하지만 JQuery와 실제로 세 개의 매개 변수와 함수를 찾고 있습니다 :

    error(jqXHR, textStatus, errorThrown)
    

    두 개 이상의 매개 변수를 추가해야합니다.

    관련 항목 : '사용 중지'언급 위의 모든 의견을 살펴 가지고하시기 바랍니다 :)

    $.ajax("www.stackoverflow.com/api/whatever", {
        dataType:"JSON"
        data: { id=1, name='example' }
    }).succes(function (result) {
        // use result
    }).error(function (jqXHR, textStatus, errorThrown) {
        // handle error
    });
    

  7. 7.jquery.com에서 :

    jquery.com에서 :

    The jqXHR.success(), jqXHR.error(), and jqXHR.complete()
    callback methods introduced injQuery 1.5 are deprecated
    as of jQuery 1.8. To prepare your code for their eventual 
    removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.
    

    전역 핸들러를 원하는 경우에 당신은 사용할 수 있습니다 :

    .ajaxStart(), .ajaxStop(),
    .ajaxComplete(), .ajaxError(),
    .ajaxSuccess(), .ajaxSend()
    
  8. from https://stackoverflow.com/questions/6792878/jquery-ajax-error-function by cc-by-sa and MIT license