-2

Im using javascripte and im trying to make a simple ajax request using an url from an api .When I open the page it returns me: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. I don't know how to fix the error, I have been trying some things but they didnt work for me. Im using: chrome. Hope you can help me. Here is the code:

 $.ajax({
   url: 'https://brokersports.ddns.net/api/v2/basketball/inlive/',
   type: 'GET',
   contentType: 'application/json',
   headers: {
     "Authorization": "Bearer my-api-key"
   },
    success: function (data) {
                        console.log(data);
                    $.each(data.matches, function( index, value ) {
                         var league ='<tr id="live"><td id="time">'+ value.live.status+'</td><td id="equi">'+value.match+'</td><td id= "score">'+value.live.score+'</td>';
                       
                          $.ajax({
                           url: 'https://brokersports.ddns.net/api/v2'+value.url+'/1X2/531',
                           contentType: 'application/json',
                           headers: {
                             "Authorization": "Bearer my-api-key"
                           },
                            success: function (data) {
                                             console.log(data);
                                             $.each(data.dataLive, function( index, value ) {
                                             var match ='<td id="odd"><button id=btn>'+ data.dataLive[0].odds[0]+'</button></td><td id="odd"><button id=btn>'+ data.dataLive[0].odds[1]+'</button></td><td id="odd"><button id=btn>'+ data.dataLive[0].odds[2]+'</button></td><td id="odd"><button id=btn>+plus</button></td></tr>';
                                              $('#basketlive').append(league+match);
                                               
                                            });
                                           },
                                           error: function (error) {
   }

                           });


                       
                  });
                   },
   error: function (error) {

   }
   });

This is the error : enter image description here

  • if the server is yours, send CORS headers - if it isn't - you can't fix it `I have been trying some things` like what? You'll need to make the request from your server on behalf of the client (proxy) – Bravo Nov 23 '20 at 08:23
  • You can not fix this from the requesting end. The site you are making your request _to_, has to explictly allow this. If they don’t, then you can not make direct client-side scripted requests to this, and you would need a _CORS proxy_ instead. – CBroe Nov 23 '20 at 08:24
  • Rather than a CORS proxy (which could disappear tomorrow) use your own server to make the request – Bravo Nov 23 '20 at 08:25
  • This is one of the most common Stack Overflow questions please check if this question was already asked. –  Nov 23 '20 at 08:28

1 Answers1

-3

This is probably something you have to fix on the server side. For me, working with node.js serverside, installing and using the cors npm library works.

LukaZ
  • 13
  • 3
  • Installing npm libraries is just a layer of abstraction. Please try not to direct people to libraries if possible –  Nov 23 '20 at 08:28
  • Libraries are great, but only if they make sense in context (and answers should show how to use them). There's no suggestion that the OP is (a) Using Node.js or (b) is running the server they are making the request to at all. – Quentin Nov 23 '20 at 08:30