0

Someone marked this post as duplicate. How can I remove it?

I am trying to hold ajax response in a variable. According to several searches online such as this one, I can't figure it out how to retain data in the variable. I am getting undefined in browser console with below code.

function callbackrequest(data) {
    return data;
}
function getDataName () {
    $.ajax({
        url: "/search/data",
        type: "POST",
        success: function(data) {
            callbackrequest(data);
        }
    });
}


var responseData = getDataName();
console.log(responseData);

I verified that responseText has data that I am looking for when I use return $.ajax, but somehow I cant retain the data in responseData variable.


My solution

function callbackrequest(data, anotherdata) {
    // Code to process data from the arguments
}
function getDataName (anotherdata) {
    $.ajax({
        url: "/search/data",
        type: "POST",
        success: function(data) {
            callbackrequest(data, anotherdata);
        }
    });
}

$(document).on('click', '.add-submit', function(evt) {
    evt.preventDefault();
    var dataarray = $('.new-data select').serializeArray();
    var datajson = createJsonFormOutput(dataarray);
    datajson["customer_id"] = $('#id').val();
    var request = $.ajax({
        url: "/api/aedit",
        data: datajson,
        type: "POST"
    }).done(function(data) {
       var returncode = $.parseJSON(data);
       if (returncode.success === true) {
          getDataName(returncode); 
       } else {
           console.log(returncode);
       }
    });
});
Community
  • 1
  • 1
Ray
  • 1,095
  • 3
  • 18
  • 43
  • Set: async: true in $.ajax options. – Danny Fardy Jhonston Bermúdez Oct 02 '15 at 23:02
  • Put your `var getdata` outside the scope of the `getDataName() {...}` function so you can access it from the `$(document).on('click')` handler. I don't recommend this approach. Refactor your code so the handler is registered after the ajax response is done. – lxe Oct 02 '15 at 23:02
  • Return the AJAX object itself in the `getDataName` function. Then listen to the promise until it is resolved, the log `names`. – Terry Oct 02 '15 at 23:04
  • @DannyFardyJhonstonBermúdez - in jquery `async: true` is by default enabled. There is no need to add that in the code. @lxe - What do you mean by refactor your code? I am kind of confuse. @Terry - I tried returning ajax and it gives me `readystate 1` with all the ajax response. Is that a right way to access `responseText`? – Ray Oct 02 '15 at 23:57
  • The original question was: «How to access ajax response without async false». – Danny Fardy Jhonston Bermúdez Oct 03 '15 at 00:54
  • `callback` is a function that responds with the arguments that contains `responseText`. It is an asynchronous request so that function is what is called on completion of the ajax request. The link tagged for the dupe has some great answers explaining this. – weeksdev Oct 03 '15 at 00:54
  • Thanks @weeksdev. I have been playing with call backs for an hour to figure it out. I have updated the code, but still having issues. :( – Ray Oct 03 '15 at 01:05
  • `responseData` will have to be assigned in the callback function and whatever action you would take with that data has to happen there to. It can't happen outside of the callback because the calls are non-blocking. – weeksdev Oct 03 '15 at 01:14
  • Thanks again @weeksdev. I see.. so there is no way for me to hold the data in global variable, all the action has to be done in the call back method then. I might have to create another argument that can hold data from another ajax request too to able to process both data in the call back. I will try it out and see how it works. – Ray Oct 03 '15 at 01:27
  • you can put the data in a global variable or whatever, but it can't happen until the ajax request responds. When you put the assignment of that variable outside the callback then it gets assigned before. – weeksdev Oct 03 '15 at 01:30
  • @weeksdev - I just added an update to my question, am I doing anything wrong? – Ray Oct 03 '15 at 01:36
  • So I was able to make it working some other way... What I did was that I passed another argument in callback and processed everything within the callback function. So that way I dont have to struggle with holding values in global values, which I never figured out how to retain ajax values in global variable. – Ray Oct 03 '15 at 02:45

0 Answers0