0

I'm just trying to get the lyrics from the Musixmatch API and console.log it out in a json format. Nothing too fancy But I End Up getting this Error: NO 'Access-Control-Allow-Origin' Header. The URL that my JS generates works.. If I copy and paste it into the browser it shows the result. But when I do fetch() It shows error. I Did a Lot of digging for this error and it seems that my browser seems to be blocking it. And all the solutions I found were for the server side. As I don't control the Musixmatch server I Can't do anything about the result not containing a header. And I am A Newb as Js.So What can I do on my side to fix this error? TIA

Here's my Code:

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="js/main.js"></script>
    <title>Document</title>
</head>
<body>
    
</body>
</html>

JavaScript:

    const api_key = '***********'


    async function GetLyrics(){
        const search = 'Where are you now when I need you most?'
        const api_link = `http://api.musixmatch.com/ws/1.1/track.search?&q_lyrics=${search}&apikey=${api_key}`;
        const result = await fetch(api_link);
        let data = await result.json();
        console.log(result)
    }
    
    GetLyrics()
XooT
  • 1
  • 2
  • That's how browser security works. If the target site does not explicitly allow a page from a different domain to access it via `fetch()` etc, then the browser gives that error. – Pointy Apr 10 '21 at 15:58
  • So it's just hat Musicxmatch doesn't want me to use `fetch()` to get their data? – XooT Apr 10 '21 at 16:02
  • The API is setup in a way so that you cannot use it from JavaScript in the browser. You need to do the API call from a server. one reason is that in order to do the call from the browser you would have to publish the api token which is not allowed. – NineBerry Apr 10 '21 at 16:47
  • Oh Man, Thanks! Your Explanation clears everything.. I remember they saying not to expose the API. I didn't think they would go to such lengths..This Really Sucks. But Thanks man! Very Appreciated! – XooT Apr 10 '21 at 17:03

0 Answers0