Problems with ajax request

0

Well, I'm trying to perform a simple ajax request, but the called content is not being displayed. How can I resolve?

var xmlhttp;

function callContent(){

    var mainContent = document.querySelector(".main-content");
    xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET", "random.php", true);
    xmlhttp.onreadystatechange = callback(mainContent);

    xmlhttp.send();

}

function callback(contentDiv){
    console.log(xmlhttp);
    if(xmlhttp.readystate == 4 && xmlhttp.status == 200){

        contentDiv.innerHTML = xmlhttp.responseText;

    }

}
    
asked by anonymous 01.12.2017 / 14:17

1 answer

1

Murilo, change this line xmlhttp.open ("GET", "random.php", true); for xmlhttp.open ("GET", "random.php", false); in this case you are saying that the call will be synchronous and you will be able to debug and see the return of your call.     

01.12.2017 / 15:43