I encountered an error while trying to fetch data from an API using JavaScript.
<button onclick="event.preventDefault(); getOdds()">Print odds</button>
JS
function getOdds() {
const sportsKey = prompt("Enter the sports key:");
const region = prompt("Enter the region (choices = uk, us, us2, eu, au):");
const market = prompt("Enter the market (choices = h2h, spreads, totals, outrights):");
const apiKey = 'APIKEY';
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Accept', 'application/json');
headers.append('Access-Control-Allow-Origin', '*');
headers.append('Access-Control-Allow-Credentials', 'true');
headers.append('GET', 'POST', 'OPTIONS');
headers.append('apikey','apikey');
fetch('https://apiurl/'+sportsKey+'/odds?apiKey='+apiKey+'®ions='+region+'&markets='+market+'', {
mode: 'no-cors',
credentials: 'include',
method: 'GET',
headers: headers
})
.then(response => response.json())
.then(json => console.log(json))
.catch(error => console.log('Authorization failed : ' + error.message));
}
Upon clicking the "Print odds" button, I encountered the following error:
Authorization failed : Unexpected end of input
How can I resolve this issue?