I know there is a litter such questions and I have tried to read them but non are quite fitting the symptoms under which I am getting blocked by CORS. Here is my simple HTML with the script that makes the HTTP GET request. I serve this index.html
using the Live Server.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Testing</title>
</head>
<body>
<script>
let url1 = 'https://api.dictionaryapi.dev/api/v2/entries/en/hello' // works fine
let url2 = 'https://evilinsult.com/generate_insult.php?type=json' // CORS Error
fetch(url2)
.then(r => r.json())
.then(json => console.log(json))
</script>
</body>
</html>
Note that both these URLs work fine when I just paste them in the browser address bar. However, while the url1
works fine, url2
gets blocked with,
127.0.0.1/:1 Access to fetch at 'https://evilinsult.com/generate_insult.php?type=json' from origin 'http://127.0.0.1:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
I understand I can do something like I checked the headers the browser sets when Access-Control-Allow-Origin: *
but why do I have to?url2
is pasted into the address bar and this specific key is not even present there.
Could someone help me understand why one fetch is successful, the other fails and both work in the browser address bar?