1

So the problem is i keep getting this error. Its very intermittent and is looking some what like a Loch Ness Monster bug. Since it is never been seen on my system i just have the error report i checked stack overflow. Only 2 questions exist like mine but with no answers. So here is the code.

$(function() {
    $.get("http://mysite.com/menu/popoutmenu1.php", {}, function(response) {
        $("body").append(response);
    })
})

When that code is executed (obviously not an exact replica of the code i am using) i get this error.

XMLHttpRequest cannot load http://www.mysite.com/menu/popoutmenu1.php. 
Origin http://mysite.com is not allowed by Access-Control-Allow-Origin.

Now this works perfectly fine on my computer, but on my friends computer (its on some website) is getting the same error! I have tried SEVERAL computers and all of them work. I am very confused on this.

If anyone has any suggestions that would be great, thanks.

1 Answer 1

2

Use a relative or root relative URL (path only, without hostname):

$(function() {
    $.get("/menu/popoutmenu1.php", {}, function(response) {
        $("body").append(response);
    })
})

Self enforced SOP compliance, if you can't do it that way you'll have SOP issues.

It looks like it may be an issue with hostname forwarding. But either way this will address it.

Sign up to request clarification or add additional context in comments.

3 Comments

Interesting... I will have to see if that works. it just does not make any sense since they are both from the same server.
The same origin policy applies not just to the same server but the same connection specifics (for instance port). In the error message that you provided one is the second level domain mysite.com, while the other has the www hostname. There is most likely a forwarding proxy that does redirects rather than being transparent to handle the different hostnames, or possibly the server is configured to use a specific host name which doesn't match what you're using. Either way the name is being changed between request and response which is enough to break the constraints of SOP.
So this is what i did (since i have several servers) All ajax queries use my namespace myNamespace.getUrl(). The nice part is my getUrl() did return mysite.com it now just uses the relative root path (depending on the server (test/stage/produciton have different ones). Thank you very much! (the getUrl() is set by the phpsies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.