javascript - 创建一个将 JSON 对象作为参数的函数
问题描述
我想从 API 中检索信息,然后创建一个将 JSOn 对象作为参数的函数。然后从返回 JSON 数据的方法中调用函数并传入 JSON 数据。到目前为止,我已经使用 fetch() 链接到 API:
let url = "https://api.magicthegathering.io/v1/cards";
fetch(url)
.then(res => res.json())
.then((out) => {
console.log("JSON", out);`enter code here`
})
.catch(err => { throw err });
有人可以帮我弄清楚其余的怎么做吗?
解决方案
假设你有一个名为“handle_data”的函数,你可以这样称呼它:
<html>
<head>
<script>
function handle_data(data) {
console.log('Data below was printed by handle_data function')
console.log(JSON.stringify(data))
}
function get_json(){
fetch('https://api.github.com/orgs/nodejs')
.then(response => response.json())
.then(data => {
handle_data(data)
})
.catch(error => console.error(error))
}
</script>
<head>
<body>
<button type="button" onclick="get_json();">Demo</button>
</body>
</html>
推荐阅读
- c - 将 libusb USB 固件移植到 Windows 10 的问题
- python-3.x - 在 zsh 中将别名更改为 python3
- python - 用于捕获语音以供 Julius 识别的 adintool 或psychopy?
- python - 烧瓶的 Apache Superset 虚拟环境安装问题
- laravel - 如何使用 Laravel Scout 实现基于用户权限的搜索功能
- python - 如何使用 PIL 获得灰度值?
- python - 从python中的字典创建postgres表
- java - 选择一个随机数来显示胜利或失败的导航游戏
- c# - 任务计划程序中的多个任务是否可以同时运行控制台应用程序(.exe 文件)?
- c# - WCF 在客户端调用服务