E
E
ennet2015-09-23 11:59:11
JavaScript
ennet, 2015-09-23 11:59:11

How can I pass value from success (ajax)?

I have an ajax request

var user = "";
               $.ajax({
                    url: Url,
                    type:"GET",
                    success:function(data) {
                       user = data.user;                    
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        console.log(xhr.responseText);
                    }
                });
                console.log (user);

console.log still returns empty.

I tried to make return from success, it did not help. How can you pass a value from success ? Tell me please.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Ukolov, 2015-09-23
@ennet

How can I make a function run only after another one has completed?

G
Gregory, 2015-09-23
@grigruss

Look like this:

var user = "";
               $.ajax({
                    url: Url,
                    type:"GET",
                    success:function(data) {
                       user = data.user;
                       console.log (user);
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        console.log(xhr.responseText);
                    }
                });
                console.log (user);

E
ennet, 2015-09-23
@ennet

Also used promises

var user = "";
$.ajax({
   url: Url,
   type:"GET",
}).done(function(data){
   console.log (data);
}).fail(function(jqXHR, textStatus, errorThrown) {
   console.log(jqXHR.responseText);
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question