简体   繁体   English

使用 MusixMatch API 获取获取结果

[英]Get fetch results with MusixMatch API

hope you're doing well I've have been learning about APIs, how to use them and when to use them, and trying to understand some concepts, but today I'm stuck希望你做得很好我一直在学习 API,如何使用它们以及何时使用它们,并试图理解一些概念,但今天我被困住了

I've been trying to use the MusixMatch API, so i went through the documentations, which was kind of self-explanatory but anytime I make a get request with fetch to an end point, my results are unusual.我一直在尝试使用 MusixMatch API,所以我浏览了文档,这是不言自明的,但是每当我发出fetch请求并到达终点时,我的结果都是不寻常的。

correct me if I'm wrong but a fetch request is a get request by default so my functions looks like this如果我错了,请纠正我,但获取请求默认是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));
}, []);

I also created a cors proxy to fix the cors error I was getting earlier so the above function is supposed to Get the artist data from their database.我还创建了一个 cors 代理来修复 cors 错误我越来越早所以上面的 function 应该从他们的数据库中获取艺术家数据。 but my results are但我的结果是

Uncaught (in promise) SyntaxError: Unexpected token '<', "<.DOCTYPE "... is not valid JSON Uncaught (in promise) SyntaxError: Unexpected token '<', "<.DOCTYPE "... is not valid JSON

image of error错误图片

and from what I can understand the res.json might have some issue but I don't know how to solve it, so I went further to just test to see what was the log message of res without the .json() , and my result showed并且据我所知 res.json 可能有一些问题,但我不知道如何解决它,所以我进一步测试以查看没有.json()res的日志消息是什么,以及我的结果显示

Response {type: 'cors', url: 'https://corsproxylyricallydemo.herokuapp.com/https…st_id=118&apikey=40cfdba86e397dc6c93070deb3a94dcd', 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

image of the error错误的图像

so I'm getting a status 200 with an empty body, I am lost, could someone please help me out?所以我的状态为200 ,身体空空,我迷路了,有人可以帮帮我吗? and also if it's not too much to ask,please recommend any resource I could read or study to understand these API concepts better cheers.而且,如果问得不多,请推荐我可以阅读或学习的任何资源,以更好地理解这些 API 概念。

You have to convert the response to either json or a text format.您必须将响应转换为 json 或文本格式。 Then you can able to access the data.然后你就可以访问数据了。 Use this用这个

.then(res => res.text())

The URL you are using for the fetch argument is wrong.您用于 fetch 参数的 URL 是错误的。 Change改变

`https://corsproxylyricallydemo.herokuapp.com/https://api.musixmatch.com/ws/1.1/artist.get?artist_id=118&apikey=${ApiKey}`

to

`https://api.musixmatch.com/ws/1.1/artist.get?artist_id=118&apikey=${ApiKey}`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM