To receive the response, you will need to include a readystatechange
event handler function. This function will be triggered once the response is available for reading:
xhr.onreadystatechange = function() {
if (this.readyState !== 4) return; // not ready yet
if (this.status === 200) { // HTTP 200 OK
alert(this.responseText);
} else {
// handle server errors here
}
};
xhr.open('GET', 'http://www.example.net/abc.aspx', true);
xhr.send();
Additionally:
("XMLHttpRequest" in window)
While using in
is typically a good way to check for property existence, it's not ideal in this case.
The issue arises with IE7+ where, even if the ‘native XMLHttpRequest’ option is disabled, XMLHttpRequest
remains as a property in window
but with a value of null
. In such cases, it's recommended to use a simple truth test for better compatibility and fallback options:
var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('MSXML2.XMLHttp');