0

I'm trying to figure out why this isn't working. I thought I understood scope...

var admin_data = false;
function getPlayerAdmin(){
    var formData = {
        'player_id' : $('#post-v').attr('data-val')
    };
    $.ajax({
        type: 'post',
        url: '/popins/player/controls',
        data : formData,
        dataType : 'json',
        success: function(admin_data) {
            if(admin_data.controls.is_admin == true){
                admin_data = true;
            }else{
                admin_data = false;
            }
        }
    });
}
alert(admin_data);

So basically admin_data is being set to true, however it doesn't change it when I alert it after. How do I pass this back down?

Thanks!

So far I have this now:

var admin_data = false;

adminData(function(admin_data) {
    if(admin_data_vals.controls.is_admin == true){
        admin_data = true;
    }else{
        admin_data = false;
    }
});

function getPlayerAdmin(adminData){
    var formData = {
        'player_id' : $('#post-v').attr('data-val')
    };
    $.ajax({
        type: 'post',
        url: '/popins/player/controls',
        data : formData,
        dataType : 'json',
        success: adminData
        },
        error : function(jqXHR, textStatus, errorThrown){
            console.log(jqXHR);
        }
    });
}
alert(admin_data);

3rd attempt:

var admin_data = false;
function getPlayerAdmin(admin_data){
    var formData = {
        'player_id' : $('#post-v').attr('data-val')
    };
    $.ajax({
        type: 'post',
        url: '/popins/player/controls',
        data : formData,
        dataType : 'json',
        success: function(admin_data_vals, admin_data) {
            if(admin_data_vals.controls.is_admin == true){
                admin_data = true;
            }else{
                admin_data = false;
            }
        },
        error : function(jqXHR, textStatus, errorThrown){
            console.log(jqXHR);
        }
    });
}
getPlayerAdmin();
alert(admin_data);
11
  • 3
    Possible duplicate of How do I return the response from an asynchronous call? Commented Nov 7, 2016 at 21:01
  • Not understanding that post, too much going on. Commented Nov 7, 2016 at 21:04
  • 1
    The gist is that you need to call a function from inside the success handler which uses the data, or use Promises. I highly recommend reading the first few answers though. You will learn a lot about how asynchronous code works. Commented Nov 7, 2016 at 21:06
  • Because the request has not been completed.. Yet you are already displaying the result.. When doing an ajax call. There is a delay before the data received. If you check the flow of your code. You're expecting that the request is already complete before it runs your alert call Commented Nov 7, 2016 at 21:08
  • Now that makes sense, thanks guys. I know about async, I just figured it applied to the function itself, I didn't know it waited and jumped to said next functions. Damn. Commented Nov 7, 2016 at 21:10

1 Answer 1

1

You can do many things here to resolve this with things like promises, deferred objects, etc., but you could simply just do your logic in the success/error callbacks if there isn't much complexity to what you need to do with the response.

success: function(adminData) { // Should probably change the parameter name
                               // to not be the same as the variable you are setting
  if (adminData.controls.is_admin == true) {
    admin_data = true;
    alert(admin_data);
  } else {
    admin_data = false;
    alert(admin_data);
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

This doesn't return the variable to be used in another function.
Sure it does, replace alert with your function
You're missing the point. I'm trying to get admin_data variable in another function. I can't load that function in the success.
Why can't you? How does your function look like?

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.