Node.js 如何发送 HTTP 请求获取 JSON 数据
是的,Node.js 可以使用内置的 'http' 模块或第三方模块如 'axios' 或 'request' 发送 HTTP 请求并获取 JSON 数据。以下是一个使用 'axios' 模块获取 JSON 数据的示例:
const axios = require('axios');
axios.get('https://example.com/api/data')
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
此代码将发送 GET 请求到 'https://example.com/api/data',并将响应数据打印到控制台。请注意,'response.data' 是一个包含 JSON 数据的 JavaScript 对象。
原文地址: https://www.cveoy.top/t/topic/obcz 著作权归作者所有。请勿转载和采集!