JavaScript fetch request issue : C-CORN [duplicate] - javascript

This question already has answers here:
Why does my JavaScript code receive a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error, while Postman does not?
(13 answers)
Closed 11 months ago.
I'm trying to run JavaScript as a snippet starting from one domain and do fetch request to a google search and I keep getting this error:
'Access to fetch at 'https://www.google.com/' from origin 'https://www.somewebsite.org' 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 tried many ways to fix it by passing headers and it did work. Not sure at this point what else I can do.`

It is the server (https://www.google.com/ in this case) of the requested resource that is responsible for and controls the CORS headers necessary. In order for you to be able to access the resource from your origin domain, the CORS policy that google serves will have to allow your website to request it.

Related

How do I fetch a the html of a website within a chrome extension [duplicate]

This question already has answers here:
How do I send an HTTP GET request from a Chrome extension?
(1 answer)
'Access to fetch has been blocked by CORS policy' Chrome extension error
(3 answers)
Closed 2 months ago.
I am trying to do the below
fetch("https://www.google.com/search?q=trees&tbm=isch&sxsrf=ALiCzsYCq08DI1a2kANT5mYBvDyYi7keWw%3A1671527330860&source=hp&biw=2025&bih=1279&ei=onuhY7uXMcaA0PEP6t2GuAo&iflsig=AJiK0e8AAAAAY6GJshVTus5cslLxfh_vqy0n5ro5Half&ved=0ahUKEwi78KPG7If8AhVGADQIHequAacQ4dUDCAc&uact=5&oq=trees&gs_lcp=CgNpbWcQAzIICAAQgAQQsQMyCAgAEIAEELEDMggIABCABBCxAzIFCAAQgAQyBQgAEIAEMggIABCABBCxAzIICAAQgAQQsQMyCAgAEIAEELEDMgUIABCABDIFCAAQgAQ6BAgjECc6CAgAELEDEIMBOgsIABCABBCxAxCDAToHCCMQ6gIQJzoFCAAQsQNQAFibFmD8FmgDcAB4AIABN4gBkAOSAQE4mAEAoAEBqgELZ3dzLXdpei1pbWewAQo&sclient=img", {
})
.then(response => {
console.log(response)
})
but this is giving me errors
Access to fetch at 'URL' from origin 'chrome-extension://...' 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 have tried using headers with 'Access-Control-Allow-Origin' but this still return the same error. I have also tried using 'no-cors'. This works but then there is no body returned and I need the HTML for parcing.
I also have the below but it did not fix the issue.
"host_permissions": [
"https://www.website.com/*"
],

Fetch API throws an error while fetching from steam web api

I want to fetch a information from steam web api's 'GetFriendsList' endpoint.When I am using fetch to do this from my front end javascript file.Im getting an error.
Access to fetch at 'https://api.steampowered.com/ISteamUser/GetFriendList/v1/?key=<apikey>&steamid=76561198364464404' from origin 'http://localhost:3001' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: 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 went to mdn and saw I must use
Access-Control-Allow-Origin: http://localhost:3000
Vary: Origin
in the fetch option.I did add these and no difference same error,I also added
mode: 'no-cors'
It worked this time but no response was returned from the steam web api.
I am really new to this.This is the first time using fetch.Any help regarding this is appriciated.THANK YOU.

No 'Access-Control-Allow-Origin': Reading CSV, D3 [duplicate]

This question already has answers here:
How does the 'Access-Control-Allow-Origin' header work?
(19 answers)
No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API
(26 answers)
Closed 3 years ago.
I am attempting to read a csv form the source https://stats.oecd.org/Index.aspx?DataSetCode=WILD_LIFE.
Let x be the url above.
<script type="text/javascript">
var data_threatened = d3.csv(x)
.then(function(data){
console.log(data);
})
</script>
However, when I run this script on my local host, I receive the following message:
Access to fetch at
'https://stats.oecd.org/Index.aspx?DataSetCode=WILD_LIFE' from origin
'http://localhost:8888' 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.
Uncaught (in promise) TypeError: Failed to fetch
Is there a way around this block?
Fetching the file and serving it from the same origin as the script runs would be the most easy. eg. PHP cares little about CORS (assuming that this file occasionally changes). In case it doesn't change, even manually placing it there would suffice.
To let a server-side script fetch the file and then serve it as same-origin, without caching it:
<?php
$url = "https://stats.oecd.org/Index.aspx?DataSetCode=WILD_LIFE";
header("Content-type: text/csv");
echo file_get_contents($url);
?>
Any server-side scripting language should be capable of doing so; I used PHP just to provide an example. One could make WILD_LIFE a variable, so that it could be used to fetch any data-set.

CORS Error on my domain [duplicate]

This question already has answers here:
XMLHttpRequest cannot load XXX No 'Access-Control-Allow-Origin' header
(11 answers)
Closed 6 years ago.
I had been developing a website and had disabled web security to bypass CORS through the following command
chrome.exe --disable-web-security --user-data-dir=/path/to/foo
After successfully developing the website, I uploaded it to my domain but got stuck with the CORS Error. I have tried adding "Access-Control-Allow-Origin": "*", but had no luck.
I can assume CORS error for localhost, but cannot understand why the error persists in my domain?
XMLHttpRequest cannot load https://xxx-xxx.xxxxx.xxxxx.xxxxx.com/. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://xxxxx.xxxxx.com' is therefore not allowed access.
To enable CORS your server has to set "Access-Control-Allow-Origin" and "Access-Control-Allow-Methods": "POST, GET, PUT, DELETE" (for example).
For a quick reference you can check the wikipedia or for an in depth info visit de w3c specification.
The third part webService must be allowed you access or they had done CORS for every origins. So you just communicate with them. Hope it can help you.

Where to add Access-Control-Allow-Origin using ReactJS?

When I try fetch data from localhost:5000 (my api is at localhost:8080) i have an error:
Fetch API cannot load http://localhost:5000/users/1. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8080' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
I know that i must add header Access-Control-Allow-Origin: * but i don't know where.
I'm using JS+Python (ReactJS and Flask frameworks)
You will have to add in your web server whichever you're using ; provided that you are taking security risk.

Categories