Javascript 获取api后填充变量
我想调用一个API,在调用它之后,我想用它的数据填充缓存。但是我不知道为什么不读取我的id来填充变量Javascript 获取api后填充变量,javascript,api,Javascript,Api,我想调用一个API,在调用它之后,我想用它的数据填充缓存。但是我不知道为什么不读取我的id来填充变量 var Acache = {}, Bcache = {} ; let path = window.location.pathname; let d = new Date(); d.setHours(0, 0, 0, 0); let startdatum = (Math.floor(d.getTime() / 1000.0)); let dl = new Date(); dl.setHour
var Acache = {}, Bcache = {} ;
let path = window.location.pathname;
let d = new Date();
d.setHours(0, 0, 0, 0);
let startdatum = (Math.floor(d.getTime() / 1000.0));
let dl = new Date();
dl.setHours(24, 0, 0, -1);
let enddatum = (Math.floor(dl.getTime() / 1000.0));
fetch(APIcall).then(
res => {
handlecache(res, Acache);
}
);
fetch(APIcall).then(
res => {
handlecache(res, Bcache);
}
);
function handlecache(res, id) {
res.json()
.then((myJson) => {
id = myJson;
console.log(myJson);
})
}
也许我个人使用的承诺+获取解决方案可以在这里帮助你
let cache;
window.onload = function () {
fetch(YOUR_API)
.then((response) => response.json())
.catch(function (error) {
console.log('Error: ' + error.message);
})
.then((myJsonResponse) => {
cache = myJsonResponse;
});
};