I’m a total noob when it comes to jQuery but I am trying to learn. What in tarnation am I doing wrong here?
function MakeCall(url) {
var result;
$.ajax({
url: url,
dataType: "text",
success: function(txt) {
result = txt;
return;
}
});
alert(result);
return result;
}
EDIT:
ok, sorry about that bit of stupidity… i’ve fixed that part. now my problem is that the alert where it is now is returning “undefined”, even though the result is getting set properly in the success function…
Thanks so much…
Apologies – I’m cross posting an answer to the question, only saw the other question this guy posted originally.
Jquery’s ajax functions are asynchronous – therefore the success function will be called after the second alert is run. It will then return from the function before the ajax method is completed.
If you really want to build the app this way, you can add an async option to the call, setting it to false. (http://docs.jquery.com/Ajax/jQuery.ajax#options, see the first entry.) This will result in the success function being called before the $.ajax method returns.
However, doing blocking ajax calls is not recommended, as it will hang the script and the browser.
Therefore, I advise you to restructure the application as such: