1

I want to give variable from JSON to global variable...

See my jquery code:

$.post("php/init.php", function(data) {
        data = JSON.parse(data);
    });
    alert(data);
    var text = $("#text").val();
});

How i can alert the "data" variable.

Hasnain Bukhari
  • 458
  • 2
  • 6
  • 23
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Sebastian Simon May 18 '18 at 12:46

1 Answers1

3

You can assign or alert the local variable in its scope.

$.post("php/init.php", function(data) {
        data = JSON.parse(data);
               alert(data);
       var text = $("#text").val();
    });

});

On callback response you can handle it.

Hasnain Bukhari
  • 458
  • 2
  • 6
  • 23