问题
Explanation:
I have a small web application running on apache server on a machine that uses javascript to do some XHR's. For a long time it worked with no problems, today all the XHR's stopped working but only on localhost, if you access it from outside it works perfectly.
Problem:
Using mozilla firefox, firebug warns:
"Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at
http://127.0.0.1:3581/datasnap/rest/TdssMloteamento/getLoteamento/true/
. This can be fixed by moving the resource to the same domain or enabling CORS."
But i'm on localhost acessing a local content that have XHR calls a local datasnap server on the same machine, resuming, locally it fails, and from web it works.
Comments:
I am acessing apache web page within url: http://127.0.0.1:3582/beeWebLoteamento/Principal.php
This is totally, really, weird for me, that just does not make sense, no-logic, why i get a cross-domain error if i'm acessing the same domain?
Objective:
I want to know what is happening and solve this problem to continue doing my XHR's locally and via the web (external) too.
回答1:
I found the solution/problem, that is:
Replaced all 127.0.0.1
to 192.168.25.100
(that is the local machine ip) and everything worked fine, so the request was:
http://127.0.0.1:3581/datasnap/rest/TdssMloteamento/getLoteamento/true/
and became:
http://192.168.25.100:3581/datasnap/rest/TdssMloteamento/getLoteamento/true/
And i acessed my web application (apache) within the URL:
http://192.168.25.100:3582/beeWebLoteamento/Principal.php
Resuming:
To avoid these cross-domain problems, use the local ip address of the machine "that usually starts with 192.168.xxx.xxx
" to access everything that are hosted on it, nor XHR either Apache, instead of using 127.0.0.1
or localhost
回答2:
try send header access-control-allow-origin: *
in your backend script
来源:https://stackoverflow.com/questions/25772001/xhr-cross-domain-error-on-the-same-domain-localhost