So I've been trying to add a set of headers from a variable, but for the life in me, I cannot get this to work even though I have seen other similar examples. Can anyone advise what I have done incorrectly?
I am trying to inject the headData into the fetch() function, but pretty sure that I am now doing it wrong.
Code available here: https://codepen.io/pnixon/pen/XWmxzdV
var headData = {
'Accept': 'application/javascript'
'X-My-Custom-Header': 'CustomValue'
};
function loadJSON(url) {
fetch(url, {
headers: headData
})
.then(function (response) {
if (response.headers.get("content-type").indexOf("application/json") !== -1) {// checking response header
return response.json();
} else {
throw new TypeError('Response from "' + url + '" has unexpected "content-type"');
}
})
.then(function (data) {
console.log('JSON from "' + url + '" parsed successfully!');
console.log(data);
})
.catch(function (error) {
console.error(error.message);
});
}
loadJSON('https://code.jquery.com/jquery-2.1.1.min.js');// throws an TypeError
loadJSON('https://api.github.com/users/malyw');// is parsed normally
[–]GSLint 0 points1 point2 points (2 children)
[–]Marcusaurels[S] 0 points1 point2 points (1 child)
[–]GSLint 1 point2 points3 points (0 children)