Problems with ajax request

Asked

Viewed 47 times

0

Well, I am 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;

    }

}
  • This.log console is showing the content correctly?

  • not either, but when I open Random.php it works perfectly. readystatechange status is 0, even if you ask for open and send

1 answer

1


Murilo, change that line xmlhttp.open("GET", "Random.php", true); to 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.

  • nothing different happened, only on the console that a warning appeared saying that you should not use synchronous Xmlhttp on the main thread

  • the file continues to be requested but is not displayed.

  • I made some console.log’s, its readystate is null whenever the request is made

  • in readystate, just put the S in upper part ... :/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.