Jquery mobile function calls before Ajax Request end

Posted by rpmlins on Stack Overflow See other posts from Stack Overflow or by rpmlins
Published on 2012-12-16T04:51:21Z Indexed on 2012/12/16 5:04 UTC
Read the original article Hit count: 119

Filed under:
|
|
|

Here is my problem.

When My Home page shows, I call the LoadUser functions, which on success, sets the globalUser variable with the returned json, and after it loads I call the alert function but it says globalUser is undefined. I have tried many other work arounds, but I always get the undefined mesage.

If I call the alert(globalUser); on the success function, it works as expected alerting the object.

$('#Home').live('pageshow', function(event) {
   $.when(LoadUser()).done(function(a1) {
     alert(globalUser);
   });
});


function LoadUser() {
$.ajax({
    // connects with the web service and validate de user input
    url: "http://localhost:51396/Icademy.asmx/GetUser",
    contentType: "application/json; charset=utf-8",
    data: { "userName": "'rodrigo'" },
    dataType: "jsonp",
    success: function(json) {
        globalUser = JSON.parse(json.d);
        return globalUser;
    },
    error: function(ret) {
        alert("Um erro ocorreu, tente novamente mais tarde.");
    }
});

}

© Stack Overflow or respective owner

Related posts about android

Related posts about jQuery