首页 > 解决方案 > 如何解决 SSL 错误获取音频路径

问题描述

我一直在尝试在 HTML 音频元素中使用来自歌词 API 的 mp3 文件 - https://lyrics.ovh 。但是我一直收到以下 cors 错误(它不会在本地发生,但是当我部署到 gitpages 时会发生)。

加载资源失败:net::ERR_CERT_COMMON_NAME_INVALID

这就是我一直试图解决的方法:

我已经尝试过这里提到的解决方案之一:请求的资源上没有“Access-Control-Allow-Origin”标头——当尝试从 REST API 获取数据时,将我自己的代理部署到 Heroku。它适用于我需要的一个 url,但对于包含 mp3 路径的那个却没有。响应返回状态:200,但我收到错误:JSON 中的意外令牌。

控制台错误:

GET http://127.0.0.1:5500/[object%20Promise] 404 (Not Found) script.js:89 Uncaught (in promise) SyntaxError: Unexpected token I in JSON at position 0

const cors = https://my-proxy.herokuapp.com


// get Mp3 
 async function getMP3(url) {
    const res = await fetch(`${cors}/${url}`, {
    headers: {
              "Content-Type": "audio/mpeg",
              Accept: "audio/mpeg",
            },
    });
    const data = await res.json();   // this line causes the error
 }

// url 示例:(代理 url + mp3 路径)

https://my-proxy.herokuapp.com/http://cdn-preview-e.deezer.com/stream/c-e6b5f2295c3af5280cc00b3bf842ff57-9.mp3

// 音频元素

<audio controls>
    <source src="${getMP3(url)}" type="audio/mpeg">       
</audio>

标签: sslfetchmp3

解决方案


ERR_CERT_COMMON_NAME_INVALID is an HTTPS error, not a CORS error. It means that the SSL certificate of the domain you're attempting to connect to is invalid.

More specifically, ERR_CERT_COMMON_NAME_INVALID means it's invalid because the SSL certificate is registered to a different domain(Or subdomain) than the domain you're attempting to connect to.


推荐阅读