<script>
    function responseCreate() {
        var html = '';
        var xhr = new XMLHttpRequest();
        xhr.open("POST", "url", true);
        xhr.setRequestHeader('Content-Type', 'application/json');
        xhr.setRequestHeader("Authorization", "xxxxx");
        xhr.send();
        recs.onreadystatechange = function () {
            if (this.readyState == 4 && this.status == 200) {
                var data = JSON.parse(this.responseText).data;
                for (var i = 0; i < data.length; i++) {
                    html += "<div>" + data[i].Full_Name + "</div>;
                }
            }
        }
        document.getElementById("ResponseView").innnerHTML = html;
    }
</script>

<div id="ResponseView"></div>

When I do console log the variable 'html' inside the anonymous function, it returns the html but then when I do console log outside the anonymous function it return empty data. Somone please help?

0

There are 0 best solutions below