[SOLVED] Exposing a feebnack value from withing an AJAX call-back function...
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Exposing a feebnack value from withing an AJAX call-back function...
Hi all,
Please consider the following snippet:
Code:
<script>
var aValue
function dothis()
{
getID("peter","pan");
console.log(aValue);
}
function getID(a,b)
{
$.post('getID.php', {aname: a, apass: b},
function (fbdata)
{
aValue = fbdata;
}
);
}
</script>
The JS function doThis() is called upon the load from a <body> tag, it in turn calls the getID() function, and AJAX call to a PHP script on the server. Though the variable aValue is declared globally, it comes back as undefined. How do I expose the results from the call-back function un the $.post call?
PLease consider I'm learningt he ropes here...
Thank you for a reply...
Thor
Then I guess it's because ajax calls are asyncronous. In your second example, do you get "undefined" in the console before the actual value? The function getID returns first, and your anonymous function is called when the response from the ajax call is ready.
Thanks for the input! In act, the whole thing does work...just...I dont know how to us it - the call is made, and the PHP works in the background, in the mean time, the JavaScript moves on, extracts the value that is yet to arrive from the server,, but before it does...it's as yet undefined. As suggested, setting the call to syncronous (async:false) solves this.
Of course, this is not what AJAX is meant for, but at this critical stage, the info is needed and the call has to halt there to wait for the info to come through...
Marking this as solved
Thor
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.