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();
当我发送请求时,谷歌浏览器中的控制台声明我的密钥丢失了。我也试图直接在标题中写入密钥(而不是作为变量),但我遇到了同样的问题。