1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-16 02:19:55 +00:00

Fix httpRequest() header handling

Header names are not case sensitive, so the old code failed if it was called with a "Content-Type" header
This commit is contained in:
jeremy@jermolene.com 2020-08-17 18:44:36 +01:00
parent c23eedd069
commit a2796d89ab

View File

@ -24,6 +24,16 @@ exports.httpRequest = function(options) {
var type = options.type || "GET",
url = options.url,
headers = options.headers || {accept: "application/json"},
hasHeader = function(targetHeader) {
targetHeader = targetHeader.toLowerCase();
var result = false;
$tw.utils.each(headers,function(header,headerTitle,object) {
if(headerTitle.toLowerCase() === targetHeader) {
result = true;
}
});
return result;
},
returnProp = options.returnProp || "responseText",
request = new XMLHttpRequest(),
data = "",
@ -63,10 +73,10 @@ exports.httpRequest = function(options) {
request.setRequestHeader(headerTitle,header);
});
}
if(data && !$tw.utils.hop(headers,"Content-type")) {
request.setRequestHeader("Content-type","application/x-www-form-urlencoded; charset=UTF-8");
if(data && !hasHeader("Content-Type")) {
request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
}
if(!$tw.utils.hop(headers,"X-Requested-With")) {
if(!hasHeader("X-Requested-With")) {
request.setRequestHeader("X-Requested-With","TiddlyWiki");
}
try {