从自我下载URL获取JSON数据

发布于 2025-01-27 18:23:54 字数 635 浏览 2 评论 0原文

我制作了一些代码,以JSON格式从URL获取数据。但是,今天我遇到了一个URL,即当您使用它时,它会自动启动下载。当我下载并使用浏览器打开它时,我会在浏览器中看到JSON格式数据。因此,要读取此数据,我必须修改我的代码才能在本地读取该下载的文件。但是我不想这样做,因为我不想将数据存储在计算机上。是否可以读取此JSON数据而无需实际下载?

编辑:@Amadan ...如果我将链接粘贴到浏览器中,然后按Enter。当然,如果我在nodejs中获取链接,我会收到一个错误消息。我的获取方法非常适合链接到JSON页面的链接,这些链接在浏览器中粘贴链接时不会触发下载。

我获取以下代码:

export async function retrieveData(url)
{
  return await fetch(url)
   .then(response => response.json())
   .then((jsonData) => {
   // jsonData is parsed json object received from url
   return jsonData;
})
.catch((error) => {
  throw error;
});}

我收到的错误消息说:未接收(在承诺中)TypeError:未能获取

I made some code that fetches data from url's in JSON format. However, I encountered today a url that when you go to it, it automatically starts a download. When I take this download and open it with a browser, I see JSON format data in my browser. So in order to read this data I have to modify my code to locally read that downloaded file. But I don't want to do that because I don't want to store the data on my computer. Is it possible to read this JSON data without actually downloading it ?

edit: @Amadan ... The downloading start if I paste the link in the browser and press enter. Of course if I fetch the link in NodeJS I get an error message. My fetch method works perfect for links to JSON pages that don't trigger a download when I paste the link in the browser.

I fetch with following code:

export async function retrieveData(url)
{
  return await fetch(url)
   .then(response => response.json())
   .then((jsonData) => {
   // jsonData is parsed json object received from url
   return jsonData;
})
.catch((error) => {
  throw error;
});}

The error message I got says: Uncaught (in promise) TypeError: Failed to Fetch

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文