首页 > 解决方案 > 在 JavaScript 中获取两个 URL(在点击监听器内)

问题描述

我想使用这两个 API,但第二个不起作用。这两个实际上是在一个点击监听器中。

 fetch('https://api.openweathermap.org/data/2.5/weather?lat=' + coord.lat + `&lon=` + coord.lng + '&units=metric&appid=xxx')
      .then(response => response.json())
      .then(data => {
        var tempValue = data['main']['temp'];
        var descValue = data['weather'][0]['main'];


        temp.innerHTML = "Temp: " + tempValue;
        desc.innerHTML = "Desc: " + descValue;
        //jamFactor.innerHTML = "Traffic jam: "

      })

    fetch('https://traffic.ls.hereapi.com/traffic/6.2/flow.json?apiKey=xxx&prox=39.8485715,-86.0969867,10')
      .then(response => response.json())
      .then(data => {
        var jfValue = data['RWS'][0]['RW'][0]['FIS'][0]['FI'][0]['CF'][0]['JF'];

        jamFactor.innerHTML = "Traffic jam: " + jfValue;
      })

标签: javascriptfetch-api

解决方案


推荐阅读