Click here to Skip to main content
15,936,565 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Problem: Fetching a different website html using javascript fetch() API from a website running on a localhost.

I need to do some scraping stuff from a local website.
I'm getting response status code "0" and no data in return to work with!
I've no idea how to make this work?

Response {type: 'opaque', url: '', redirected: false, status: 0, ok: false, …}
body: null
bodyUsed: false
headers: Headers {}
ok: false
redirected: false
status: 0
statusText: ""
type: "opaque"
url: ""
[[Prototype]]: Response

What I have tried:

When I tried this first I got CORS warning then I changed the mode to 'no-cors' but still it's not working.

Index.js File:

'use strict';

fetch('', {
    method: 'GET',
    mode: 'no-cors',
.then(res => console.log(res))
.catch(err => console.log(err));

Need help!!
Updated 28-Feb-22 2:11am

This is precisely as the documentation says it should be:
no-cors — Prevents the method from being anything other than HEAD, GET or POST, and the headers from being anything other than simple headers. ... In addition, JavaScript may not access any properties of the resulting Response. This ... prevents security and privacy issues arising from leaking data across domains.
Specifying mode:'no-cors' is not a magic bullet to circumvent the cross-origin restrictions specified by the target site.

Unless you can get the cooperation of the owners of the target site, you will need to create a proxy script on your own server to request the remote resource.
Share this answer
prajapatiabhishek31 28-Feb-22 8:34am    
Thanks for answering! I'm new to CORS concept so I'm still trying to understand it but as far as I've understood that it won't work without my server getting whitelisted by the source itself. am right on that?

One more thing, when I try the same thing with a node.js script I get the html file. why is that?

const axios = require('axios').default;
.then(res => console.log(res))
.catch(err => console.log(err));
Richard Deeming 28-Feb-22 9:10am    
Code running on the client won't be able to make cross-domain requests unless the target server explicitly allows it.

Code running on the server, including node.js, is not subject to the same restrictions.
prajapatiabhishek31 28-Feb-22 9:13am    
Thanks a lot!
You may want to investigate using the YouTube Data API  |  Google Developers[^].
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900