I am trying to update a line of text in my website with a value from a simple JSON file on another website. This is a slightly different question than the ones I've seen posted on stack - the reason being that I can't use JQuery (this is a school project), and I'm not supposed to use any back-end things, like Node/PHP/Angular/etc. I was wondering if it could be done with vanilla javascript?
I've tried a few different methods and the following gets me what I want, but I've had to use a chrome extension (Allow CORS) to work around the CORS error:
let url1 = "https://www.codewars.com/api/v1/users/jrd656"
let h = new Headers();
h.append('Accept', 'application/json');
let req = new Request(url1, {
method: 'GET',
headers: h,
mode: 'cors',
});
fetch(req)
.then((response)=>{
if(response.ok){
return response.json();
}else{
throw new Error('BAD HTTP stuff');
}
})
.then( (jsonData) =>{
console.log(jsonData);
})
.catch( (err) =>{
console.log('ERROR:', err.message);
});
The chrome extension only solves the issue for my browser and I want this to work for all browsers. I understand I could use a proxy, but this would require me to learn some back-end php/Node/etc, and I'd like to avoid that if possible.
I believe there is a way to overcome the CORS issue with:
Access-Control-Allow-Origin: *;
, but when I paste this in my .js file it doesn't work, and my browser gives me an error Uncaught SyntaxError: Unexpected token ':'
.