Created
March 4, 2018 21:48
-
-
Save TheGreatRambler/6b0db0af9c52acc406689f587b0b4bc8 to your computer and use it in GitHub Desktop.
Get headers of file from ajax request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function returnheaders(ajaxrequest) { | |
var headers = ajaxrequest.getAllResponseHeaders(); | |
var arrofheaders = headers.trim().split(/[\r\n]+/); | |
var mapofheaders = {}; | |
arrofheaders.forEach(function (line) { | |
var parts = line.split(': '); | |
var header = parts.shift(); | |
var value = parts.join(': '); | |
mapofheaders[header] = value; | |
}); | |
return mapofheaders; | |
} | |
// Usage | |
$.ajax({ | |
src: "test.txt" | |
}).done(function(data, textStatus, jqXHR) { | |
var headers = returnheaders(jqXHR); | |
console.log(headers); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment