1

I need to store some data outside ajax call in a variable. So, I am using async: false option in ajax. But it gives me an error:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.

See my code below:

var internal_tags = null;

$.ajax({
    async: false,
    dataType:'json',
    contentType:'application/json',
    url: '/ajax-search-suggestions',
    type: 'GET',
    success: function(data) {
        asignVariable(data);
    }
});

// get internal tags
function asignVariable(data){
    internal_tags = data;
}

I need to save ajax success data into internal_tags variable. If I remove the option async: false, data are not saved in variable. So how I can remove the error or how to save ajax success data in the variable which is outside of ajax.

6
  • dont use async false Commented Jan 24, 2017 at 7:30
  • 1
    Not only you should be getting an error, but your keyboard should be taken away for even considering using async: false so that you never ever repeat this mistake again. Commented Jan 24, 2017 at 7:30
  • Why do you need async: false? Commented Jan 24, 2017 at 7:31
  • so what should I do. How I can achieve my requirements Commented Jan 24, 2017 at 7:31
  • You should pass a callback function to your helper method. Welcome to the AJAX world. This world is asynchronous and the sooner you start thinking in terms of callbacks rather then passing return variables from your functions, the better for you :-) Commented Jan 24, 2017 at 7:31

1 Answer 1

1

You should not use async: true. You should refactor your code so that your functions do not return values but rather take callbacks.

Example of what you should not be doing:

var internal_tags = null;

function getValue() {
    $.ajax({
        async: false,
        dataType:'json',
        contentType:'application/json',
        url: '/ajax-search-suggestions',
        type: 'GET',
        success: function(data) {
            internal_tags = data;
        }
    });
}

And here's the correct approach:

function getValue(callback) {
    $.ajax({
        dataType:'json',
        contentType:'application/json',
        url: '/ajax-search-suggestions',
        type: 'GET',
        success: function(data) {
            callback(data);
        }
    });
}

so instead of:

var internal_tags = null;
getValue();
alert(internal_tags);

you should do this:

getValue(function(internal_tags) {
    alert(internal_tags);
});

I also recommend you looking at jQuery's deferred objects.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your comment. Problem is, I want to use internal_tags from ajax as parameter of other functions. For example: I am using typeahead jquery library where I want to use the internal_tags as parameter. i.e. source: substringMatcher(internal_tags),
So you will initialize the typeahead plugin inside the callback and not outside like you are doing it right now. It is only inside this callback that you have the result of the asynchronous call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.