【发布时间】:2021-05-18 13:59:02
【问题描述】:
我正在尝试向此 api 发出客户端获取请求: 'api.openweathermap.org/data/2.5/weather?zip=' 这里我们需要传递 zip+apiKey 来获得响应:
在应用程序中尝试这样做,但我在“获取请求”中收到错误,表明 url 或获取路由错误:
/* Global Variables */
let baseURL = 'api.openweathermap.org/data/2.5/weather?zip=';
let apiKey = '####';
let appID='&appid=';
// Create a new date instance dynamically with JS
let d = new Date();
let newDate = d.getMonth()+'.'+ d.getDate()+'.'+ d.getFullYear();
document.getElementById('generate').addEventListener('click', performAction);
function performAction(e){
const temperature = document.getElementById('zip').value;
const userResponse = document.getElementById('feelings').value;
const getTemp = async (baseURL, temperature, appID,apikey)=>{
const res = await fetch(baseURL+temperature+appID+apikey)
try {
const data=await res.json();
return data;
} catch(error) {
console.log("error", error);
// appropriately handle the error
}
}
getTemp().then( res =>{ console.log(res);
postData('/add', {temperature: temperature, date: newDate, userResponse: userResponse});
updateUI('/all');})
https://codesandbox.io/s/romantic-leaf-0bvpd?file=/src/index.html
【问题讨论】:
-
添加 URL 的协议部分:
https://api... -
它在那里并给出了同样的错误,所以我删除了它
-
我注意到“不安全” - 使用
http://api... -
@RandyCasburn 是一样的
-
错误是什么?还是坏网址?使用与浏览器图像中显示的完全相同的 URL,看看是否仍然出现错误 - 但请确保包含 URL。
标签: javascript node.js