1

I have been researching this problem for like 2 days with no solution on sight. Also I have no idea why this had to be so difficult. I am trying to catch data from another site, to localhost. It is not allowing me whatever I try to do. I searched for 2 days, modified apache header to header('Access-Control-Allow-Origin: *'); In PHP. Also in .htaccess. I researched other answers, on stack and other sites with absolutely no change.

Here is my default.js file:

 $.ajax({
     type: "GET",
     url: "http://anothergames.com",
     dataType: 'json',   
     cache: false,
     success: function(data)
      {
        console.log('hello world!');
      } 
  });

Here is index.php:

<?php
header('Access-Control-Allow-Origin: *');  

echo '<!doctype html>
<html>
    <head>
        <title>Marrja e te dhenave per udhetimet turistike nga website nepermjet Ajax</title>
        <link rel = "stylesheet" href = "default.css">
        <meta content="text/html; charset=UTF-8; X-Content-Type-Options=nosniff" http-equiv="Content-Type" />
    </head>
    <body>
        Hello World!
        <script src = "js/jquery.min.js"></script>
        <script src = "js/default.js"></script>
    </body>
</html>';

Also changed localhost to something else. Still nothing. Tried other solutions, but new problems arise, with mime type mismatch and forced mime type.

If you have any idea, let me know. Am using xampp in windows 10 and Chrome. This is the error I get.

enter image description here

GSquadron
  • 204
  • 3
  • 12
  • header("Access-Control-Allow-Headers","*");header('Access-Control-Allow-Credentials', true); header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); – RïshïKêsh Kümar Apr 22 '17 at 19:33
  • @RïshïKêshKümar OP can't add headers on another site output. read the question – charlietfl Apr 22 '17 at 19:35
  • Unless the other site api is `CORS` enabled or their api supports `jsonp` you need to use a proxy, either on your server or third party service. You can't set those headers on your site ... they must be set in the response by remote site – charlietfl Apr 22 '17 at 19:37
  • If the remote site is not allowing you to make direct requests because of CORS your best bet is to put some kind of proxy between it. You can create a little PHP proxy script that fetches the the data from the remote server and forwards it to your web application. – Thakkie Apr 22 '17 at 19:40

1 Answers1

1

chrome does not support localhost for CORS requests there's here's a link to the bug : https://bugs.chromium.org/p/chromium/issues/detail?id=67743

what you should do is use a proxy, something like this:

https://github.com/Rob--W/cors-anywhere

Jazib
  • 1,343
  • 13
  • 27