0

This is my Javascript code.

<script>
        var result2;
        $(document).ready(function () {
            $("#driver").click(function (event) {
                $.getJSON('data.json',
                    function(jd, index) {
                        var result = jd.DevEUI_uplink.FCntUp;
                        result2 = result;
                    });     
            });
        });
        alert(result2)
    </script>

But it return undefined.

please help.

  • 2
    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) – str Nov 11 '18 at 11:47
  • You are alerting before the click or the ajax request even occur – charlietfl Nov 11 '18 at 11:47
  • as @cherlietfl mentionned, why not put alert inside the callback. Javascript probably reaches your `alert` before the callback is executed. – zakaria amine Nov 11 '18 at 11:52

0 Answers0