0

It's a commonly asked question (see numbered links at end), but I can't work out how to solve it.

I include my javascript code below. I'm using MAMP PRO, and in the htdocs folder I've added a file '.htaccess' with contents 'Header set Access-Control-Allow-Origin "http://localhost:8888"' and reset the MAMP server but I get the same error.

Can someone suggest where I might be going wrong? Let me know if you need more info, I'm very new to this..

function loadComments(callback){ 
if (window.XMLHttpRequest) {
  xmlhttp = new XMLHttpRequest();// code for IE7+, Firefox, Chrome, Opera, Safari
} else {
  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");// code for IE6, IE5
}

xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
if (JSON.parse(this.responseText)[0]==null) {

        } else {
alert(JSON.parse(this.responseText));
        }

    }
};
xmlhttp.open("GET","http://localhost:8888/projectX/projectX.php?webpageURL=" + "https://theguardian.com/au" ,false);
xmlhttp.send();
}
  1. The 'Access-Control-Allow-Origin' header has a value that is not equal to the supplied origin error
  2. The 'Access-Control-Allow-Origin' header has a value 'https://dev.getevents.co' that is not equal to the supplied origin
  3. The 'Access-Control-Allow-Origin' header has a value that is not equal to the supplied origin
  4. The 'Access-Control-Allow-Origin' header has a value 'http://localhost:4200' that is not equal to the supplied origin
scon
  • 59
  • 1
  • 6
  • 1
    You are making a request **to** `http://localhost:8888`. That is not your origin. The origin is most likely the domain that is in your address bar. – Ivar Jul 23 '20 at 07:29
  • Hey Ivar, this isn't a website that is live (this is one that I am testing locally, the URL is file:///Users/...projectX.html'), sounds like that is relevant to the question? If so, I will edit my question to clarify that point.. – scon Jul 23 '20 at 07:32
  • It depends on how you are opening the page that is making this AJAX request. What is the domain? Does it start with `file://`? – Ivar Jul 23 '20 at 07:36
  • Yes, it does start with file://. Is it the case that I can't request my mamp server to retrieve info from a website (in this case theguardian) and echo it back if the website isn't live? If so, how do you test these kinds of cross-domain requests before going live? – scon Jul 23 '20 at 07:39
  • Most likely. Your probably want your page to be served via your MAMP server. Assuming that your API is also being served by MAMP, the origin will be the same so you don't need to setup any CORS headers at all. – Ivar Jul 23 '20 at 07:43
  • Ah, awesome - thankyou. I didn't even know that was possible. So now I'm loading the page via the mamp-served localhost address, I don't get the same error. I get a different one, but I'm pretty happy just to be on to a new error. Thanks again, I was battling. – scon Jul 23 '20 at 07:47
  • `// code for IE6, IE5`...you're not really still supporting those browsers are you? I'd suggest that this block of code is probably redundant... – ADyson Jul 23 '20 at 08:37
  • Thanks ADyson. No intention to support those browsers. – scon Jul 24 '20 at 08:26

1 Answers1

0

Ivar was presumably right. Once I got MAMP to serve my javascript page as well as the php this error didn't recur.

scon
  • 59
  • 1
  • 6