获取API->未捕获(承诺)TypeError:无法获取

时间:2020-08-06 07:20:01

标签: javascript

我在获取api数据时遇到问题。此api-> https://resmush.it/api

我的请求在邮递员上工作,但在我的网站上却没有此错误:

未捕获(承诺)TypeError:无法获取

我的代码:

const params = {
    method: 'GET',
    mode: 'no-cors',
    headers: {
        'Access-Control-Allow-Origin': '*',
        'Content-Type': 'application/json'
    }
};

fetch('http://api.resmush.it/ws.php?img=https://upload.wikimedia.org/wikipedia/commons/thumb/7/77/Google_Images_2015_logo.svg/1200px-Google_Images_2015_logo.svg.png&qlty=50', params)
    .then(reponseBis => reponseBis.json())
    .then(dataBis => console.log(dataBis));

感谢帮助!

2 个答案:

答案 0 :(得分:0)

编辑:此标题<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">似乎引起了麻烦。如果将其删除,该代码应该可以正常工作

此元标记会将HTTP升级到HTTPS,但是您使用的API不提供HTTPS

From the docs:

请注意,如果所请求的资源实际上无法通过HTTPS获得,则请求将失败而不会回退到HTTP。

-------

Mixed Content: The page at 'https://www.mywebsite.com/page.php' was loaded over HTTPS, but requested an insecure resource 'http://api.resmush.it/ws.php?img=https://upload.wikimedia.org/wikipedia/commons/thumb/7/77/Google_Images_2015_logo.svg/1200px-Google_Images_2015_logo.svg.png&qlty=50'. This request has been blocked; the content must be served over HTTPS.

问题在于此链接https://upload.wikimedia.orgHTTPS,而http://api.resmush.itHTTP。将resmush网址更改为https://或将Wikimedia链接更改为http://

---------

看看HTTP Headers

使用no-cors只能用于将数据发布到不允许cors的API。但是这样做将返回空响应。您提供的API允许使用cors,因此完全不需要。

Content-Type标头应为Accept

const params = {
    method: 'GET',
    headers: {
        'accept': 'application/json'
    }
};

fetch('http://api.resmush.it/ws.php?img=https://upload.wikimedia.org/wikipedia/commons/thumb/7/77/Google_Images_2015_logo.svg/1200px-Google_Images_2015_logo.svg.png&qlty=50', params)
.then(response => response.json())
.then(json => console.log(json))
.catch(e => console.error(e));
{
  "src": "https://upload.wikimedia.org/wikipedia/commons/thumb/7/77/Google_Images_2015_logo.svg/1200px-Google_Images_2015_logo.svg.png",
  "dest": "http://par3.static.resmush.it/a95239e7e37bade3af9ca3b7eabb8f11/1200px-Google_Images_2015_logo.svg.png",
  "src_size": 48597,
  "dest_size": 19545,
  "percent": 60,
  "output": "json",
  "expires": "Thu, 06 Aug 2020 09:52:12 +0200",
  "generator": "reSmush.it rev.2.0.6.20200328"
}

答案 1 :(得分:0)

对于测试,我只使用了大部分代码:

const params = {
    method: 'GET',
    headers: {
        'accept': 'application/json'
    }
};

fetch('http://api.resmush.it/ws.php?img=https://upload.wikimedia.org/wikipedia/commons/thumb/7/77/Google_Images_2015_logo.svg/1200px-Google_Images_2015_logo.svg.png&qlty=50', params)
    .then(responseBis => responseBis.json())
    .then(json => console.log(json))
    .catch(e => console.error(e.message));

我有这个错误: Mixed Content: The page at 'https://www.mywebsite.com/page.php' was loaded over HTTPS, but requested an insecure resource 'http://api.resmush.it/ws.php?img=https://upload.wikimedia.org/wikipedia/commons/thumb/7/77/Google_Images_2015_logo.svg/1200px-Google_Images_2015_logo.svg.png&qlty=50'. This request has been blocked; the content must be served over HTTPS.

Failed to fetch
(anonymous) @ galerie.js:11
Promise.catch (async)
(anonymous) @ galerie.js:11