尝试使用 api/oEmbed 从 Vimeo 上的剪辑获取数据时出现 CORS 问题

Jea*_*ote 2 javascript vimeo fetch cors

我正在使用 fetch 来获取数据。就像这样:

  getClipMetadata = (url) => {
    const endpoint = 'http://www.vimeo.com/api/oembed.json';

    fetch(`${endpoint}?url=${encodeURIComponent(url)}`, { 
      method: 'get',
      cache: 'no-cache', 
      mode: 'cors',
      headers: new Headers({
        'Access-Control-Allow-Origin': '*',
        'Content-Type': 'application/json'
      })
    })
      .then((response) => { return response.json();})
      .then((res) => console.log("async response received", res))
      .catch((err) => console.log("ajax error -> ", err))    
  }
Run Code Online (Sandbox Code Playgroud)

所以我得到的错误是这样的:
Response for preflight is invalid (redirect)

我认为从 Vimeo 的开发者页面来看它看起来很简单。

我究竟做错了什么?

Jea*_*ote 5

端点是'https://vimeo.com/api/oembed.json'而不是'http://www.vimeo.com/api/oembed.json',我发送的标头也引起了问题。

所以最终的工作代码如下所示:

  getClipMetadata = (url) => {
    const endpoint = 'https://vimeo.com/api/oembed.json';

    fetch(`${endpoint}?url=${encodeURIComponent(url)}`, { 
      method: 'get',
      cache: 'no-cache', 
      mode: 'cors',

    })
      .then((response) => { return response.json();})
      .then((res) => console.log("async response received", res))
      .catch((err) => console.log("ajax error -> ", err))    
  }
Run Code Online (Sandbox Code Playgroud)