Rob--W / cors-anywhere

CORS Anywhere is a NodeJS reverse proxy which adds CORS headers to the proxied request.
MIT License
8.61k stars 6.02k forks source link

Complete steps for implementing cors-anywhere for personal server #113

Open harshdamaniahd opened 6 years ago

harshdamaniahd commented 6 years ago

I have taken this project locally and run node

PS E:\cors-anywhere-master> node server Running CORS Anywhere on 0.0.0.0:8080

Then when i try to access the site http://localhost:8080/https://posthuset.eurest.no/dagens-meny/?day=mandag I get error Missing required request header. Must specify one of: origin,x-requested-with

Rob--W commented 6 years ago

CORS Anywhere offers a proxy for cross-origin requests. It does intentionally not allow direct requests, e.g. when you open the above URL in the location bar.

To test whether it works, send a cross-origin request with XMLHttpRequest / fetch or any of your favorite AJAX libraries. If you want to initiate a request from the same origin, set the X-Requested-With request header to satisfy the requirement.

harshdamaniahd commented 6 years ago

I tried in IE console, i am getting error

fetch("http://localhost:8080/http://posthuset.eurest.no/dagens-meny/?day=mandag") .then((res) => { return res.text(); }).catch((error)=>{ debugger; console.log(error); }) .then((data) => { console.log(data); })

Missing required request header. Must specify one of: origin,x-requested-with

Rob--W commented 6 years ago

What is the domain from where you are testing the above code? It must be different from http://localhost:8080/ to be a cross-origin request (for example, try http://127.0.0.1:8080/).

harshdamaniahd commented 6 years ago

It is working in chrome but not working in IE. I tried using both localhost and http://127.0.0.1:8080/.

image

Rob--W commented 6 years ago

By "IE", do you mean Internet Explorer or Edge? Your example with fetch suggests that you are probably using Edge (because IE does not support fetch). If you use a polyfill, then be aware that IE9 and earlier does not properly support CORS.

You might be affected by https://stackoverflow.com/questions/20784209/internet-explorer-11-does-not-add-the-origin-header-on-a-cors-request

In that case, a work-around is to set the X-Requested-With header (e.g via the headers parameter in fetch).

harshdamaniahd commented 6 years ago

Its not working in edge as well as IE in edge it gives the following error : SEC7111: HTTPS security is compromised by http://127.0.0.1:8080

I change the code by adding X-Requested-With , but it is still not working

fetch("http://127.0.0.1:8080/http://posthuset.eurest.no/dagens-meny/?day=mandag",{

  headers: new Headers({
    'X-Requested-With': 'XMLHttpRequest'
  })

})
.then((res) => {
      return res.text();
}).catch((error)=>{
  debugger;
  console.log(error);
})
.then((data) => {
  console.log(data);
})

Also when i run node server it shows me

PS E:\cors-anywhere-master> node server Running CORS Anywhere on 0.0.0.0:8080

Rob--W commented 6 years ago

That looks like a mixed content warning. Try testing from http.

harshdamaniahd commented 6 years ago

you mean the site in which i am running the code is https and not http.

For example my site is : https://ten.abc.com and in this site I am executing the Fetch Api code

harshdamaniahd commented 6 years ago

I tried simple google.com as well it doesn't work. When i try running it in browser , it gives me the below

image

harshdamaniahd commented 6 years ago

In chrome i need to enable this : Load unsafe scripts image