0

I try to collect the html text from different sites and after that to search if those sites contain my pub-id. The problem is that I can't acces other websites, becase of Access-Control-Allow-Origin.

Failed to load http://example.com/: Redirect from 'http://example.com/' to 'http://example.com/' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:50000' is therefore not allowed access.

Here you can take a look at my code:

var site = 'http://example.com';
var pub_id = 'pub-27638960xxxxxx';
var found = -1;

$.get(site, function(data, status){
        alert('Data: ' + data + '\nStatus: ' + status);
        found = data.indexOf(pub_id);
 if(found >=0)
 {
         alert('Found at position: ' + found);
 }
    });

This code is add in my Construct 2 project. What is more interesting is that if I will run/preview my game in NW.js, the code will work. If I will preview/run in Chrome, Firefox, Edge, etc, the code will NOT work.

Somebody knows how to solve this? I understand that I need to make a request to that server, but I don't know how.

Thank you!

PS: If you know other way to make this in Javascript or Construct 2, I'm all ears.

Stefan
  • 49
  • 1
  • 6
  • Possible duplicate of [Origin http://localhost:3000 is not allowed by Access-Control-Allow-Origin](https://stackoverflow.com/questions/18642828/origin-http-localhost3000-is-not-allowed-by-access-control-allow-origin) – Kapsonfire Jul 06 '18 at 10:33
  • Possible duplicate of [Why does my JavaScript get a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error when Postman does not?](https://stackoverflow.com/questions/20035101/why-does-my-javascript-get-a-no-access-control-allow-origin-header-is-present) – Ivar Jul 06 '18 at 10:34
  • The keyword(s) here is _"from different sites"_. Their servers needs to allow for your domain to do that, and if they won't you aren't suppose to get it. An option would be to use server side web client acting as a browser and call those sites. – Asons Jul 06 '18 at 10:36
  • That's the problem. I need to search for my pub-id in the different site where my game is. Depend on that, if my pub-id is there I will unlock my game to that site, and if my pub-id isn't there, I block the game to that site, and display a message to inform the webmaster what to do to unlock the game for his website. Is there a way to do that? – Stefan Jul 06 '18 at 10:44

1 Answers1

1

The requesting server has to SET the 'Access-Control-Allow-Origin' to your origin domain:

in php header('Access-Control-Allow-Origin: http://localhost:500');

Kapsonfire
  • 1,013
  • 5
  • 17
  • Thank you for the response. The problem is that I don't have access to all the sites. Like I said in the post comment: "That's the problem. I need to search for my pub-id in the different site where my game is. Depend on that, if my pub-id is there I will unlock my game to that site, and if my pub-id isn't there, I block the game to that site, and display a message to inform the webmaster what to do to unlock the game for his website. Is there a way to do that? " Thank you! – Stefan Jul 06 '18 at 10:45
  • If you cannnot access the server you are requesting you should setup a proxy... Like you call your own server and get the content with curl – Kapsonfire Jul 06 '18 at 11:11
  • Can you be more specific? Or to give me an example? I'm super new in Javascript, like 5 days old! :)) – Stefan Jul 06 '18 at 15:31