Get Raw Http Response From Ajax
Using plain JavaScript AJAX in a browser, can I get the raw HTTP response from the server? By that I mean headers and body as raw text like: < HTTP/1.1 301 Moved Permanently <
Solution 1:
You will need to combine two calls to get the body and the response headers.
- To get the headers, use
getAllResponseHeaders
- To get the body, use property
.response
when the readystate is 4 (=DONE)
var request = new XMLHttpRequest();
request.open("GET", "http://www.example.com", true);
request.send(null);
request.onreadystatechange = function() {
if (request.readyState == 4){
console.log(request.getAllResponseHeaders());
console.log(request.responseText);
}
};
Post a Comment for "Get Raw Http Response From Ajax"