Wait for jQuery $.get function to finish before running code

前端 未结 3 1672
伪装坚强ぢ
伪装坚强ぢ 2020-12-19 23:12

In the following, the return statement is being executed before the call to $.get() finishes. How can I change this?



        
3条回答
  •  暖寄归人
    2020-12-20 00:10

    The callback code by @Adam Rackis is working for me. In fact, if the content of the url is of json structure, you can directly refer the fields like this. Note that aft = { yourToken :'' } is just dummy since I am not using the value in this dictionary at all. I just want to get the user_id from the URL.

    var aft = { yourToken: '' };
        aft.setToken = function(url, callback){
            $.get(url, function(data) {
                user_id = data[0]['user_id'];
                if (callback)
                    callback(data);
            });
        };
    
        aft.setToken("https://XXXXXX.azurewebsites.net/.auth/me",  function(dataReturned){
            alert("You user id is " + labeler);
    

提交回复
热议问题