The XMLHttpRequest method setRequestHeader() sets the value of an HTTP request header.
When using setRequestHeader(), you must call it after calling open(), but before calling send().
If this method is called several times with the same header,...
var xhr = new XMLHttpRequest();
xhr.open('GET', '/server', true);
xhr.onload = function () {
// Do something with the retrieved data ( found in xmlhttp.response )
};
xhr.send(null);
xhr.send() doesn’t return anything
The response is in onload
Another way:
The XMLHttpRequest
response property returns the response's body content as
an ArrayBuffer, a Blob, a Document,
a JavaScript Object, or a string, depending on the value
of the request's responseType
property.
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
callback(xhr.response);
}
}
1 Like