0
    var nRequest = new XMLHttpRequest();
var nutritionalAPIData;
var phrase = document.getElementById('foodSearchBar');
var link = 'https://nutritionix-api.p.mashape.com/v1_1/search/' + phrase + '?fields=item_name%2Citem_id%2Cbrand_name%2Cnf_calories%2Cnf_total_fat';
var mashapeKey = 'My key would be written here';
console.log(link);
JSON.stringify(link);
console.log('Button Works');
    nRequest.open('GET', link);
    nRequest.setRequestHeader('X-APP-KEY', mashapeKey);
    nRequest.setRequestHeader('Content-Type', 'application/json');
    nRequest.onload = function getInfo(){
        nutritionalAPIData = JSON.parse(nRequest.responseText);
        console.log(nutritionalAPIData);
        localStorage.setItem('foodInfo', nutritionalAPIData);
    }
    nRequest.send();

When I send the request, the console in google chrome states my key is missing. I've also attempted to write the key in the header directly (not as a variable), yet I get the same issue.

Raman Kaur
  • 65
  • 4
  • Side note, `link` is string, so what's the point of `JSON.stringify(link)`? – Racil Hilan Apr 07 '18 at 19:33
  • Oh I added that cause I had the variable in the link – Raman Kaur Apr 07 '18 at 19:35
  • The variable doesn't make a difference. The result will still be string. Anyway, that line `JSON.stringify(link)` is doing nothing as you're not assigning the value to a variable or using it in anyway, so it's lost. – Racil Hilan Apr 07 '18 at 19:39

0 Answers0