问题
How is it possible that when I try to make an XMLHTTPRequest from a javascript file to a web service located on the very same domain, I get:
Origin http://mydomain.com is not allowed by Access-Control-Allow-Origin ???
If I change mydomain.com to localhost, I no longer have problems, but I would like to keep it as mydomain.com
What do I have to edit, and why in the first place, since it's the same domain?
回答1:
1) look in your request/response headers. Is the server returning a weird access-control-allow-origin
header?
2) It's not just the domain, it's also the protocol (http vs https vs ftp etc), the port (if you have a :8080 or something like that), and potentially the part after the .com/
回答2:
It probably is because of the missing www. If you test with yourdomain.com and your site is at www.yourdomain.com it is concidered cross-domain and cross-domain calls are not allowed by default.
来源:https://stackoverflow.com/questions/8576252/access-control-allow-origin-problems-on-the-same-domain