xmlhttprequest error handling 1

xmlhttprequest error handling

const xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
       console.log( xhttp.responseText );
    }
};
// Error Handling:
xhttp.onerror = function(error){
	console.error( error );
}
xhttp.open("GET", "filename", true);
xhttp.send();

Here is what the above code is Doing:
1. Create a new XMLHttpRequest object.
2. Define the function to be called when the readyState property changes.
3. Open a request to the specified URL.
4. Send the request.

Similar Posts