2016-09-01 19 views
4

現在、私のサーバーからHTTP 413:リクエストエンティティの処理が非常に大きいエラーを実装しようとしています。私がやったことはこれです:エラーが発生したときにJQuery AJAXエラーコールバックが呼び出されない

$.ajax({ 
    url: "submit.php", 
    data: { 
     "data": POSTData 
    }, 
    success: function(response, statusText, XHR) { 
     console.log(XHR.status + ": " + response); 
     resolve(); // resolve the promise and continue on with execution 
    }, 
    // Added this part: 
    error: function(response, statusText, XHR) { 
     if(XHR.status === 413) { 
      // Request entity too large 
      // To solve this we split the data we want to upload into several smaller partitions 
      // and upload them sequentially 

      console.log("Error 413: Request entity too large. Splitting the data into partitions..."); 

      // handling code below 

      // blahblahblah 
     } 
    }, 
    method: "POST" 
}); 

しかし、その代わりにエラーコールバックが発射さのないハンドラがありませんかのように、私のコンソールはまだ、(それが413だと言う)エラーがスローされます。どのようにこの機能を実装しますか?

+0

してみてください。http://craftcms.stackexchange.com/questions/2328/413-request-entity-too-large-error-with-uploading-a-fileまたはこれはhttp://stackoverflow.com/questions/37489351/413-request-entity-too-large-error-during-file-upload-php – vaso123

+0

@karacsi_maciありがとう、間違いなく良い選択です。私はできるならそれをクライアント側で扱うことにしますが、それが難しいと判明した場合は、サーバーの設定を変更する必要があります。 – Bluefire

答えて

3

エラーコールバックのメソッドシグネチャが間違っています。これらのドキュメントに従ってhttp://api.jquery.com/jquery.ajax/

正しい署名を参照してくださいです: 機能(jqXHR jqXHR、文字列textStatus、文字列errorThrown)あなたのケースではそのため

あなたが呼ばれてきたものXHRがあるのでXHR.statusが存在しません。実際には文字列です。

これを試してみてください:

error: function (jqXHR, textStatus, errorThrown) { 
    if(jqXHR.status === 413) { 
     // Request entity too large 
     // To solve this we split the data we want to upload into several smaller partitions 
     // and upload them sequentially 

     console.log("Error 413: Request entity too large. Splitting the data into partitions..."); 

     // handling code below 

     // blahblahblah 
    } 
}, 

は、私は強くエラーコールバックあるが呼び出されていることを疑うが、あなたはその外に何のコードをしたないためif文、あなたは何を見ていません。

+0

ありがとう!私はそれを試してみます。私はちょうど 'エラー'の署名が '成功'のものと同じであると仮定しました... – Bluefire

+0

Brilliant!私はまだコンソールでエラーが発生していますが、少なくともコールバックは起動しています。ありがとうございました! – Bluefire

+1

コンソールのエラーとは何ですか?ほとんどのブラウザはもちろん、コンソール上でHTTPエラーを報告しますが、実際にはあなたのコントロールではありません。エラーコールバックの目的は、アプリケーションがコンソールエラーを排除するのではなく、正常に処理できるようにすることです。 P.S.答えが受け入れられたとしてマークするのを忘れないように助けてくれたなら:-) – ADyson

1

jQuery.ajaxSetup()を使用すると、この種のエラーを1か所で1回だけ処理できます。この方法でHTTP 413以上の処理が可能です。

コード:これをチェックする

$(function() { 
    $.ajaxSetup({ 
     error: function(jqXHR, exception) { 
      if (jqXHR.status === 0) { 
       console.log('Not connect.n Verify Network.'); 
      } else if (jqXHR.status == 404) { 
       console.log('Requested page not found. [404]'); 
      } else if (jqXHR.status == 413) { 
       console.log('Error [413]: Request entity too large. Splitting the data into partitions...'); 
      } else if (jqXHR.status == 500) { 
       console.log('Internal Server Error [500].'); 
      } else if (exception === 'parsererror') { 
       console.log('Requested JSON parse failed.'); 
      } else if (exception === 'timeout') { 
       console.log('Time out error.'); 
      } else if (exception === 'abort') { 
       console.log('Ajax request aborted.'); 
      } else { 
       console.log('Uncaught Error.n' + jqXHR.responseText); 
      } 
     } 
    }); 
}); 
+1

これは素晴らしい解決策ですが、私がそこに持っているいくつかのデータを扱う私の現在の状況に合わないものの、間違いなく、私は後で気付くでしょう。 – Bluefire

関連する問題