我正在做一个使用3D打印和octoprint的个人项目,在这个项目中,我试图从web界面检索值,例如完成3D打印的剩余时间。我做了研究,似乎我必须做一个HTTP GET请求。我曾研究过octoprint的软件,发现他们有一个documentation接口。此外,还有一个API密钥可以访问我的octoprint的特定实例。我遇到的问题是,我从来没有编写过任何有请求的东西。我已经做了基本的javascript,但没有太高级的东西。我正在寻找一个人来指导我在做一个请求和从网络界面拉取价值的正确方向。谢谢您抽时间见我。
发布于 2019-10-30 14:28:42
按照文档中的说明访问retrieve your API key。
从这里开始,开始使用fetch是一条可行的道路。这是用于getting the API version的未经测试的代码,但应该很接近:
const response = await fetch('http://your-device-url/api/version', {
headers: {
'Content-Type': 'application/json',
'X-Api-Key': 'your key goes here'
}
});
const json = await response.json();
console.log('here is the version information', json);以下是了解有关fetch的更多信息的一些链接
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch https://javascript.info/fetch https://medium.com/codingthesmartway-com-blog/fetch-api-introduction-to-promised-based-data-fetching-in-plain-javascript-620e54898d8e
异步函数是一个相关的主题,也是你想要学习的另一件事。
https://developers.google.com/web/fundamentals/primers/async-functions
https://stackoverflow.com/questions/58618974
复制相似问题