AJAX
AJAX: Asynchronous JavaScript and XML
<script>
var xhr = new XMLHttpRequest();
var url = "//example.com";
xhr.open("GET", url);
xhr.onreadystatechange = function() {
if (xhr.status === 200 && xhr.readyState === 4) {
document.querySelector("body").innerHTML = xhr.responseText
}
}
xhr.send();
</script>
function postXML(xmlDoc) {
var req;
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.open(method, serverURI);
req.setRequestHeader('content-type', 'text/xml');
req.onreadystatechange = function() {
if (req.readyState === 4) {
if (req.status === 200) {
var result = req.responseXML;
} else {
// An error happened. Display an error dialog.
}
}
};
req.send(xmlDoc);
}
Notice that in the above code, we check for error condition by examining req.status which holds the HTTP status of the response. The below code is unrelated, but it is used to handle any error:
window.onerror = function handleError(message, URI, line) {
return true; // this will stop the default message
}
See:
- https://www.xml.com/pub/a/2005/05/11/ajax-error.html - done reading
page revision: 3, last edited: 19 Nov 2017 04:22