【发布时间】:2020-10-14 15:05:14
【问题描述】:
我一直在尝试在 HTML 音频元素中使用来自歌词 API - https://lyrics.ovh 的 mp3 文件。但是我一直收到以下 cors 错误(它不会在本地发生,但是当我部署到 gitpages 时会发生)。
加载资源失败:net::ERR_CERT_COMMON_NAME_INVALID
这就是我一直试图解决的方法:
我已经尝试过这里提到的解决方案之一:No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API,将我自己的代理部署到 Heroku。它适用于我需要的一个 url,但对于包含 mp3 路径的那个却没有。 响应返回状态:200,但我收到错误:JSON 中的意外令牌。
控制台错误:
GET http://127.0.0.1:5500/[object%20Promise] 404(未找到) script.js:89 Uncaught (in promise) SyntaxError: Unexpected token I in 位置 0 的 JSON
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>
【问题讨论】:
-
这个问题解决了吗?如果是,并且回答对您有帮助,请将其标记为“已接受”。否则,如果您需要更多说明,请直接说出来。