我试着打印weatherData,如URL中的图像所示,但是它不在控制台中打印出来。问题是什么,我该如何纠正呢?
发布于 2022-01-07 15:55:57
您的代码有两个问题(除此之外,它是作为屏幕快照出现的):
response对象。data事件只传递一个数据块;可能有几个,然后是一个end事件。https.get(url, function(response) {
console.log(response.statusCode);
var weatherData = "";
response.on("data", function(data) {
weatherData += data.toString();
}).on("end", function() {
console.log(JSON.parse(weatherData));
});
});发布于 2022-01-09 22:34:31
到目前为止,下载最简单的方法是使用Windows本机Curl命令,您可以通过Cmd控制台窗口运行该命令来保存响应文件,然后查询该response.txt
首先确认您的url是有效的(单击下面的链接)
如果您使用的是windows 10或11,那么只需在cmd行尝试使用本机版本的curl (,注意,,您需要url中的每个&使用类似于^&的^转义)或使用"URL“(所以是完整的双引号),但是在shell命令中嵌套时要小心。
curl -o response.txt https://api.openweathermap.org/data/2.5/weather?q=Paris^&appid=536bcef96b2f01cd9b9f076db90807fe^&unit=metric
type response.txt您可以将两者都包括在一行中,但对于最后一个&不需要^转义的&type
curl -o response.txt https://api.openweathermap.org/data/2.5/weather?q=Paris^&appid=536bcef96b2f01cd9b9f076db90807fe^&unit=metric&type response.txt下载之后,您应该可以在控制台中看到response.txt。
因此,您可以任意调用该命令(hidden>nul与否),或者在屏幕上或在您选择的任何应用程序中读取文本文件。
Response.txt
{“feels_like”:2.3488,"lat":48.8534},“天气”:{“id”:804,“主”:“云”,“描述”:“阴云”,“图标”:“04n”},“基座”:“台站”,“主”:{“临时”:277.1,"feels_like":277.1,"temp_min":275.8,"temp_max":278.23,“气压”:1009,“湿度”:98}、“能见度”:10000、“风”:{“速度”:1.03、“德格”:0}、“云”:{“所有”:100}、"dt":1641768056、"sys":{"type":2、"id":2012208、"country":"FR“、”日出“:1641714129、”日落“:1641744764}、”时区“:3600、"id":2988507、"name":"Paris”、"cod":200}
https://stackoverflow.com/questions/70623525
复制相似问题