0

I found a similar topic where the user gave a reply that 'window' name is needed before the variable name. But it doesn't seem to work for me. Could someone help me understand why window.data_from_ajax printed in console.log() is undefined?

PHP


<?php
    echo 'php_data';
?>

Javascript


window.data_from_ajax;

$.get('java_test.php', function(data) {
  window.data_from_ajax = data;
  alert(data);
});

console.log('data_from_ajax='+window.data_from_ajax);
stark
  • 2,246
  • 2
  • 23
  • 35
arrowman
  • 424
  • 6
  • 19
  • 1
    You need to do some reading about an **"asynchronous response"** (in the duplicate). The callback gets called sometime LATER, long after your `console.log()` statement. For an async callback, the ONLY place you can use that response is inside the callback or in some function you call from within the callback. The timing of the callback is indeterminate so that's the only place you can reliably use the result. This is async programming and is a technique to learn and use. – jfriend00 Jan 28 '16 at 07:24
  • Thank You :-) I understand this now. The main problem was that I din't know about asynchronous. So I didn't know what a question should looks. What do You think, should I delete my question? – arrowman Jan 28 '16 at 10:30
  • 1
    No need to delete it. It's been taken care of by marking as a duplicate. – jfriend00 Jan 28 '16 at 22:23

0 Answers0