JQuery Ajax with ajaxSuccess

From CoderLearner
Jump to: navigation, search

Description

Whenever the Ajax has completed successfully, it will trigger the $.ajaxSuccess() method. All the events are registered in the $.ajaxSuccess() method will executed.

Example 1

<!DOCTYPE html>
<html>
   <head>
      <title>ajax</title>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <script 
         type="text/javascript" 
         src="/lib/jquery-1.7.2.min.js">				 
      </script>
      <script>
         $(document).ready(function() {
                  
            $('#content_progress').ajaxSuccess(function(){
               $(this).html('Call Ajax Success');
            });
      
            $('form[name="frm_userinfor"]').submit(function(){
               var agev = $('input[name="age"]').val();
               $.ajax({
                  type: 'POST',
                  url: 'age',
                  data: 'age='+agev,
                  success:function(data){
                     if(data=='failed'){
                        $('#content').html('Something wrong, please try again!');
                     }
                     else {
                        $('#content').html(data);
                     }
                  }
               });
               return false;
            });
         });
      </script>
   </head>
   <body>
      <div id="content_progress"></div>
      <div id="content"></div>
      <form name="frm_userinfor" method="post">
         Age:<input type="text" name="age"/><br/>
         <input type="submit" name="check" value="Check" />
      </form>
     
   </body>
</html>

Example 2

<!DOCTYPE html>
<html>
   <head>
      <title>ajax</title>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <script 
         type="text/javascript" 
         src="/lib/jquery-1.7.2.min.js">				 
      </script>
      <script>
         $(document).ready(function() {
            
            $('#content_progress').ajaxSuccess(function(e, xhr, settings) {
            
               $(this).text('Triggered ajaxSuccess handler. The ajax response was:' 
                  + xhr.responseText );
               
            });
            $('#record').load('myrecord.txt');
            $('form[name="frm_userinfor"]').submit(function(){
               var agev = $('input[name="age"]').val();
               $.ajax({
                  type: 'POST',
                  url: 'age',
                  data: 'age='+agev,
                  success:function(data){
                     if(data=='failed'){
                        $('#content').html('Something wrong, please try again!');
                     }
                     else {
                        $('#content').html(data);
                     }
                  }
               });
               return false;
            });
         });
      </script>
   </head>
   <body>
      <div id="content_progress"></div>
      <div id="content"></div>
      <form name="frm_userinfor" method="post">
         Age:<input type="text" name="age"/><br/>
         <input type="submit" name="check" value="Check" />
      </form>
      <div id="record"></div>
   </body>
</html>

Try It