2016-12-05 3 views
0

これに似た質問がありますが、解決策が私にとってうまくいかないようです。私はいくつかの情報を取得するためのHTTP GETsサーバーをJavaアプリケーションがありますが、このサーバーが応答しない状況をテストしています。私はHTTP 403(禁止された)応答を返すWebサーバーに到達しようとしています。このエラーを処理したいと思います。Ajaxエラーハンドラが実行されていません

まず、次のコードを使用して、HTTP GETを実行してウェブページにアクセスできます。それだけです。私はHTTP 403エラー応答を取得しません。 しかし、次のコードでタイムアウトを解消しないと、ajaxはエラーになり、 'timeout'エラーで応答します。はい、それはタイムアウトエラーですが、原因HTTP 403エラーに、私はすべてのヘルプは非常に高く評価されるだろう403

init: function (credentials) { 
       $.ajax({ 
        type: "GET", 
        url: this.serverURL, 
        //timeout: 5000, 
        headers: { 
          Accept: "application/json" 
        }, 
        contentType: "application/json; charset=utf-8", 
        crossDomain: true, 
        cache: false, 
        data: { 
          'username': credentials.username, 
          'key': credentials.key 
        }, 
        jsonp: false, 
        dataType: 'jsonp', 
        //never enters here... 
        statusCode: { 
         404: function() { 
          alert('404 page not found'); 
         }, 

         400: function() { 
          alert('400 bad request'); 
         }, 
         403: function() { 
          alert('403 forbbiden'); 
         }}, 

        error: function (XMLHttpRequest, errorThrown) { 
         handleError(XMLHttpRequest, errorThrown); 
        } 
       }); 
     function handleError(XMLHttpRequest, errorThrown) { 

      //If not found, internal server error or forbidden (it should enter here) 
     if (XMLHttpRequest.status == 404 || XMLHttpRequest.status == 500 || XMLHttpRequest.status == 403) { 
      Logger.loginfo('never enters here...'); 
     } 
     else if (XMLHttpRequest.readyState == 4) { 
      //http error 
      Logger.loginfo('Error: ' + errorThrown); 
     } 
     else if (XMLHttpRequest.readyState == 0) { 
      // Network error, not possible to open() (i.e. connection refused, access denied...) 
      Logger.loginfo('Error: ' + errorThrown); 
     } 
     else { 
      //http error 
      Logger.loginfo('Error else!: ' + errorThrown); 
      // something after initializing/opening the HTTP GET but before completing it 
     } 
    };} 

取得したいと思います。

答えて

0

例外に以下を追加して、すべての値が適切に戻っているかどうかを確認する必要があります。

error: function(xhr, textStatus, error){ 
    console.log(xhr.statusText); 
    console.log(textStatus); 
    console.log(error); 
    //then call your function and pass the values 
    //and do the checks 
} 

また、この回答はdetailsで確認できます。

+1

こんにちはPritam、私は3つのログにタイムアウトエラーが発生しています....私はタイムアウトを使用しない場合、私は決してログを読むことはありません。私は見ていくつもりです、リンクに感謝します。 – agonza1

関連する問題