Ajax response not working

Ajax response not working with jQuery and php, AJAX JSON response not working, Ajax response problem, Ajax response gives null value, Ajax response value not available in function, Long ajax call break code process.

Trying to call Ajax caller function and want to use Ajax response in next level of code but Ajax response not getting in next process. Here is solution for how to get Ajax response in next process.

Above is common issue when asynchronous Ajax call takes long time and next code execution continues.

Solution for use Ajax calls response in code:

When you fire Ajax call and it not just for update data on server but you want to get data from server and use it in next process. There is two ways you can achieve it.

  1. Change code sequence
  2. Synchronous AJAX calls (Not recommended)

1. Change code sequence

function getAjaxData(){
    var resp;

    $.ajax({
        url: '.....',
        success: function(data){
            //Process your data here
            alert(data);
            //OR call callback function for next process
            callBackFunc(data);
        }
    });
}

2. Synchronous AJAX calls (Not recommended)

function getAjaxData(){
    var resp;

    $.ajax({
        url: '.....',
        async: false,
        success: function(data){
            resp = data;
        }
    });

    return resp;
}