ajaxError(callback) trong jQuery

Định nghĩa ajaxError(callback) trong jQuery

Phương thức ajaxError(callback) trong jQuery đính kèm một hàm để được thực thi bất cứ khi nào một AJAX Request thất bại. Đây là một Ajax Event.

Cú pháp ajaxError(callback) trong jQuery

Sau đây là cú pháp cho ajaxError(callback) trong jQuery:

$(document).ajaxError( callback )

Tham số

Dưới đây miêu tả chi tiết về các tham số được sử dụng trong phương thức ajaxError(callback) trong jQuery:

  • callback − Hàm để được thực thi. XMLHttpRequest và các thiết lập để được sử dụng cho request đó được truyền như là các tham số cho hàm này. Một tham số thứ ba, một đối tượng exception, được truyền nếu một exception xuất hiện trong khi xử lý request.

Ví dụ ajaxError(callback) trong jQuery

Sau đây là ví dụ đơn giản minh họa cách sử dụng của phương thức ajaxError(callback) trong jQuery:

<html>
   <head>
      <title>The jQuery Example</title>
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>


      <script type="text/javascript" language="javascript">
         $(document).ready(function() {
            $("#driver").click(function(event){
               /* Assume result.text does not exist. */
               $('#stage1').load('../result.text');
            });


            $(document).ajaxError(function(event, request, settings ){
               $("#stage2").html("<h1>Error in loading page.</h1>");
            });
         });
      </script>


   </head>


   <body>


      <p>Click on the button to load result.text file:</p>


      <div id="stage1" style="background-color:blue;">
         STAGE - 1
      </div>


      <div id="stage2" style="background-color:blue;">
         STAGE - 2
      </div>


      <input type="button" id="driver" value="Load Data" />


   </body>


</html>


Bình luận