Click here to Skip to main content
16,016,501 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i save ajax response to a global variable in javascript/jquery to be used later. I have tried several methods including saving the response text (data.responseText) returned from the call but at the end it is still empty/undefined.

I know saving the response in a DOM element is strongly discouraged.I don't want to make the same call every time i need i need that particular data. Is there no easy way of accessing it outside the ajax success call back function?

What I have tried:

var jsonResponse = $.ajax({
                url: 'PageMethod/GetData',
                method: 'post',
                dataType: 'json',
                data: JSON.stringify({ dataId: "xxx" }),
                contentType: 'application/json',
                success: function (data) {
                    
                    // not sure if i should return the data here on at end of the ajax call definition
                    return data.d.responseText;
                },
                error: function (ex) {
                    alert(ex.responseText);
                }
            }).responseText;

            alert(jsonResponse);
Posted
Updated 24-Jan-18 23:53pm

1 solution

The main (but not only) reason your code doesn't work is because the ajax call is asynchronous, that effectively means it happens in the background, or in parallel with the rest of the code. So code execution doesn't stop at the ajax call and wait for a response, so you call ajax which goes off and does its thing, your code then advances to the alert but the ajax call hasn't finished yet.

You can make ajax wait for a response before continuing by adding "async:false" as an option to the call, but that's generally not needed. To store the result you'd do something like

var myResponse;
$.ajax({
                url: 'PageMethod/GetData',
                method: 'post',
                dataType: 'json',
                data: JSON.stringify({ dataId: "xxx" }),
                contentType: 'application/json',
                success: function (data) {
                    myResponse = data.d.responseText;
                },
                error: function (ex) {
                    alert(ex.responseText);
                }
            });


myResponse is now a variable you can use anywhere, but as I said you have to understand the nature of asynchronous calls and what they're doing, that variable is only usable *after* the success has been called so you need to bear that in mind when it comes to using the variable. If you have a process that relies on this data you usually do something in the success event to kick that process off as you know at that point the variable has data.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900