How to Execute the function after Ajax call is complete

13,652

Solution 1

As you know Ajax is to make Asynchronous request. It means client no need to wait till you get the response from server. So during this Ajax object goes different states (i.e. from 0 to 4). Those are:

  • 0: request not initialized
  • 1: server connection established
  • 2: request received
  • 3: processing request
  • 4: request finished and response is ready

So as per your requirement there is two solution:

  • either you make synchronous request (but it is not recommended)
  • you need to call your 2nd function when readystate value is 4

Refer below code, it will help you:

function changeLayout() {
    try{
        jQuery.ajax({
            url:'<%=resourceURL.toString()%>',
            dataType: "text",
            data:{
                <portlet:namespace  />cmd:'changeLayout'
            },
            type: "post",

            beforeSend: function(){
                //before send this method will be called
            },
            success: function(data) {
                //when response recieved then this method will be called.
            }
            complete: function(){
                //after completed request then this method will be called.
            },
            error: function(){
                //when error occurs then this method will be called.
            }
        });
    }catch (e) {
        alert(e);
    }
}

Hope this will help you :)

Solution 2

Try this but i have created example change the code as per your requirement

function exapndCollapse(){

    //execute your script here
    //if u want reload the page means write your script here

}

function changeLayout () {
var myval = 'test';
    $.ajax({
          url:'script.php',
          type: "POST",
          data: {"myval":myval},
          success: function(data){
              //call the after ajax success 
              exapndCollapse(); //whatever the function call 
          },error: function() { 
              console.log("error");
          } // This is always fired
   });   
};    
Share:
13,652
PVBRaju
Author by

PVBRaju

Updated on June 04, 2022

Comments

  • PVBRaju
    PVBRaju almost 2 years

    I have two functions,in 1st function having ajax call,so i want the 2nd function to be execute after ajax call (here i am reloading the page)complete,can you please help me?
    Here the below code

    <div id="exapnd_or_collapse_div" class="exapnd_or_collapse_div" onclick="changeLayout();">
    <script>
    function changeLayout () {
        alert('change layout');
    
        try {
            jQuery.ajax({
                url:'<%=resourceURL.toString()%>',
                dataType: "text",
                data:{
                    <portlet:namespace  />cmd:'changeLayout'
                },
                error: function(){
                 alert("AJAXError");
                },
                type: "post",
    
                success: function(data) {
    
                    if("success" == data) {
    
                        location.reload();
                    }
                }
            }).done(function(){
                exapndCollapse(); 
              });
    
    
        } catch(e) {
            alert('waiting: ' + e);
        }
    };
    
    function expandCollapse() {
    
    
                jQuery("div.Search_Styled-dropDown").css("display", "none");
                jQuery("span.menu-text").css("display", "none");
    
                jQuery("ul.Common_ul").css("display", "none");
                jQuery("img.module_images").css("width", "25px");
                jQuery("img.module_images").css("margin-top", "20px");
            jQuery("img.module_images").css("height", "21px");
                jQuery("#search_box").css("display", "none");   
            jQuery("#Escaped_toggle").css("margin-right", "94%");   
            }
    </script>
    
  • Chandan Prakash Sharma
    Chandan Prakash Sharma about 9 years
    Try to use portlet preferences to save the state of your object. As you are trying to reload the page so you will lose all the states of the object. So whatever you perform after it, is not going to reflect @PVBRaju