按钮单击事件内未调用send()事件后的代码

时间:2019-02-24 21:08:23

标签: javascript jquery html ajax

对不起,一般来说,我对javascript和jquery还是陌生的。我是Windows应用程序开发人员,所以请多多包涵。

调试器显示了3个对象,但是由于某种原因#animal-info没有被替换?

我的Javascript:

var ourRequest;

function loadAjax(){

debugger;
ourRequest = new XMLHttpRequest();
var ourData;
ourRequest.open('Get','https://learnwebcode.github.io/json-example/animals-1.json');

debugger;
ourRequest.onload = function(){
    debugger;
    ourData = ourRequest.responseText;
    debugger;
    }
}

$(document).ready(function (){
    debugger;

    $("#myButton").click(function(){
        debugger;
        loadAjax();
        debugger;
        ourRequest.send();
        debugger;
        if (typeof(ourData) != 'undefined'){
            debugger;
            $('#animal-info').text = ourData[0];
        }
    debugger;
    }
    );

});

Here's the html:

    <!DOCTYPE html>
    <html>
        <head>
                <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
            <title>
                Json Jquery Ajax Call
            </title>
        </head>
        <body>
            <header>
                <button id="myButton">Fetch info for 3 animals</button>
            </header>
            <div id = "animal-info">Test</div>
            <script src="testfetch.js"></script>
        </body>

    </html>

0 个答案:

没有答案