0
I’m having some problems when performing an ajax request by javascript, however, it appears that the variable xmlhttp
was not defined, even though I defined it before the function and before using it. How to solve ?
var xmlhttp;
function generateMessage(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = callbackChampMessage();
var url = "randomName.php";
xmlhttp.open("GET", url, true);
xmlhttp.send();
};
function callbackChampMessage(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
var messageHeader = document.querySelector("#champ-message");
champMessage.innerHTML = xmlhttp.responseText;
};
};
- Giving a.log console(xmlhttp) I receive all zeroed or null values
so now stopped presenting errors but still not working :/
– Murilo Melo
It may be that the
status code
your page is different from200 OK
. Add, in the functioncallbackChampMessage
, oneconsole.log(xmlhttp);
before theif
.– Valdeir Psr
@Murilogambôa, I edited my answer. Passed beaten previously.
– Valdeir Psr
Just removing the parentheses from the callback call worked, I had seen this error and forgot to remove
– Murilo Melo