【问题标题】:Client side request to opengraph.io [duplicate]对opengraph.io的客户端请求[重复]
【发布时间】:2021-09-18 00:38:27
【问题描述】:

我正在尝试在客户端使用 opengraph.io。他们有 jQuery 的文档,我的代码基于: https://www.opengraph.io/examples/jquery-opengraph-example/

      const url = 'http://cnet.com';
      const urlEncoded = encodeURIComponent(url);
      const apiKey = 'MY-KEY';
      const requestUrl = 'https://opengraph.io/api/1.1/site/' + urlEncoded + '?app_id=' + apiKey;
      fetch(requestUrl).then(res => {
        console.log(res);
        console.log(res.body);
      });

我从res 得到的回复似乎是成功的,但没有我需要的数据。

body: (...)
bodyUsed: false
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: ""
type: "cors"
url: "https://opengraph.io/api/1.1/site/http%3A%2F%2Fcnet.com?app_id=MY-KEY"

我尝试注销res.body,但它只是返回:

ReadableStream {locked: false}

我在这里发现了一个类似的问题,但 API 似乎返回了 res.data,这对我来说不是这样:

How to get Open Graph meta from URL input on the client side

【问题讨论】:

    标签: javascript fetch


    【解决方案1】:

    您需要添加 res.json() 以将 ReadableStream 转换为一些 JSON。

    例如,使用无效键,只是为了显示输出:

    const url = 'http://cnet.com';
    const urlEncoded = encodeURIComponent(url);
    const apiKey = 'MY-KEY';
    const requestUrl = 'https://opengraph.io/api/1.1/site/' + urlEncoded + '?app_id=' + apiKey;
    
    fetch(requestUrl)
      .then(res => res.json())
      .then(json => console.log(json));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-23
      • 1970-01-01
      • 2023-03-28
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多