希望你做得很好--我一直在学习API,如何使用它们,何时使用它们,并试图理解一些概念,但今天我陷入了困境
我一直在尝试使用MusixMatch API,所以我浏览了这些文档,这似乎是不言自明的,但是每当我向fetch提出一个get请求时,我的结果都是不寻常的。
如果我错了,请纠正我,但是在默认情况下,获取请求是一个get请求,所以我的函数如下所示
useEffect(() => {
fetch(
`https://corsproxylyricallydemo.herokuapp.com/https://api.musixmatch.com/ws/1.1/artist.get?artist_id=118&apikey=${ApiKey}`
)
.then((res) => console.log("res", res.json()))
.then((data) => console.log("data", data))
.catch((err) => console.log(err));
}, []);我还创建了一个cors代理来修复我之前得到的cors错误,所以上面的函数应该是从他们的数据库中获取艺术家的数据。但我的结果是
SyntaxError:意外标记'<',<!DOCTYPE“.不是有效的JSON
据我所能理解,res.json可能有一些问题,但我不知道如何解决,所以我进一步测试了一下,看看没有.json()的res的日志消息是什么,我的结果显示
Response {type: 'cors', url: 'https://corsproxylyricallydemo.herokuapp.com/https…st_id=118&apikey=${API-KEY}', redirected: false, status: 200, ok: true, …}
body: (...),
bodyUsed: false,
headers: Headers {},
ok: true,
redirected: false,
status: 200,
statusText: "OK",
type: "cors",
url:"https://corsproxylyricallydemo.herokuapp.com/https://api.musixmatch.com/ws/1.1/artist.get?artist_id=118&apikey=${API_KEY}",
[[Prototype]] : Response所以我得到了一个空荡荡的200,我迷路了,有人能帮帮我吗?此外,如果这不是太多的要求,请推荐任何资源,我可以阅读或学习,以理解这些API概念更好的干杯。
发布于 2022-09-06 01:30:31
您必须将响应转换为json或文本格式。然后你就可以访问数据了。使用这个
.then(res => res.text())发布于 2022-09-06 01:38:01
用于获取参数的URL是错误的。变化
`https://corsproxylyricallydemo.herokuapp.com/https://api.musixmatch.com/ws/1.1/artist.get?artist_id=118&apikey=${ApiKey}`至
`https://api.musixmatch.com/ws/1.1/artist.get?artist_id=118&apikey=${ApiKey}`https://stackoverflow.com/questions/73615571
复制相似问题