Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取api后填充变量_Javascript_Api - Fatal编程技术网

Javascript 获取api后填充变量

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

我想调用一个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.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;
    });
};