Help with a javascript callback
January 16, 2011 6:01 PM Subscribe
Help with javascript callbacks.
I'm quite new to javascript and trying to understand how callbacks work.
I have a function that posts some values to a .php file, and then receives the values. I want to set one of these values to a variable that the rest of my program can access. The strange thing (to me) is that this does not work the first time I click the button to run this code, but it does work the second time. I'm using the JQuery library.
My code is as follows:
$.post("getLatLong.php", { latUser: 10, longUser: 20},
this.success = function(arrayData){
arrayValue = arrayData[2];
}, 'json');
arrayValue is defined at the top of my JS script, so I assumed if I set this value with the $.post(), it would be readable later in the program.
Thanks in advance for your help! I've spent too long puzzling over this.....
posted by a womble is an active kind of sloth to computers & internet (9 answers total) 2 users marked this as a favorite
$.post("getLatLong.php", { latUser: 10, longUser: 20},function(arrayData){
arrayValue = arrayData[2];
}, 'json');
posted by lsemel at 6:12 PM on January 16, 2011 [1 favorite]