I have been working on a XML, HTTP communication , and I do not understand why my code seems to have some type of error. When sending a get request, sometimes it fails to get response or even to send to the server(I believe, as no message is shown). When I include alerts it does work. I understand this is due to asynchronous programming, as the alerts give more time to the operation. But even so, I am working with onreadystatechange() so I do not understand. Here is the code:
<script type ="text/javascript">
const xhr = new XMLHttpRequest();
function sendData() {
const getEnter = document.getElementById('p_input').value;
const cont = document.getElementById('cont');
var url = "http://192.168.1.132:4344?";
url += getEnter;
xhr.onreadystatechange = () => {
if(xhr.readyState==4 && xhr.status==200){
console.log("success");
}
}
xhr.open('GET',url,true);
cont.textContent ="processing";
xhr.send();
}
</script>
I am calling this function on the onClicked event of a button. It would be excellent if someone knew how to fix this