Hey, I'm sorry if this is a noob question, but I'm trying for a day now without getting any further so I decided to give it a go here. I've made this function to get some ajax data from a page with a certain 'guid'. my function should give me the html of that page to do something else with. In the middle of my code, my var result still has all the data, but as soon as I end my function, the var is null. Does anyone have an idea what I should do? Thanks in advance! function getimagepage(i_guid) { var result = null; myframeworks.get('ajax/view/image', { data: { guid: i_guid }, success: function(data) { result = data; console.log(result); //in console, all my data is perfectly there }, error: function() { alert('Error occured'); } }); //return result; console.log(result); //in console, all my var is null }; $(document).ready(function() { getimagepage(60); }); Code (JavaScript):
I think you're missing how the order of execution works. Ajax calls are typically asynchronous -- a big fancy word meaning "running separately from the code you call it". This means that "success" method might not run until LONG after that return or console.log would be run. Generally speaking since JavaScript is single threaded the AJAX request won't even START until AFTER the function you call it in even finishes. You could try to force it to synchronous so it sits there with it's thumb up it's backside waiting for the AJAX call to finish, but that can be unstable, hog the browser, make some browsers like FF bitch about the page taking too long... just avoid that. I could probably explain better if you weren't using that mouth-breathing halfwit nonsense known as jQuery... but pretty much you need to chain whatever events or processing of data you want to do in that "success" or "error" methods. The result will not be set until that point as :success WILL NOT BE RUN until AFTER that "getimagepage" function exits. It's called "event driven programming" and is one of the hardest aspects to wrap your head around in modern programming. (by modern I mean anything after 1984). You get so used to things executing in the order you declare them the idea of "deferring" a command to run later (or even more confusing in parallel) can be very counter-intuitive at the start. I believe jQ has a method tucked away to force it to be synchronous, but in my experience just say no to that. jQuery pissing on how JavaScript even works certainly not helping with that confusion as relying on it means you'll never learn how anything REALLY works.
Sorry, got it to work on a different way without using the function. A much more simple ajax call, anyway. Thanks for you effort. But if you have such a hate towards jQuery, it's probably better not to read and answer on the jQuery forum at all ;-) Nice day and thanks.
It's always nice to provide a solution, if you have one. Others might Google, and see your question, and it's nice to provide an answer. So, if you don't mind, would you post the code you ended up with?