Problem when performing XHR on the same domain (localhost)

Asked

Viewed 1,434 times

5

Explanation:

I have an application running on a client’s computer, it worked normally for some time, then something happened that the system could only be accessed from outside and locally no longer worked.

Problem:

XHR is unable to be performed and in firefox firebug returns me the following error:

"Blocked request between origins: the same origin policy prohibits reading remote resources on http://localhost:3581/datasnap/rest/TdssMloteamento/getLoteamento/true/. This can be fixed by moving the resource to the same domain or by activating CORS."

Which is VERY strange, because to my understanding this error could occur if accessed from a different domain "outsider" and not on the contrary, which would be the case for the local host working perfectly, and on the localhost it no longer works

How do we fix this? What’s really going on? because the logic of giving error of different origin does not apply since the system is running on the client and the client itself of the server’s own computer does not access it.

2 answers

1

Not only the domain, but the protocol (ex: http vs https) and the door must be equal. To solve this problem, it is necessary to add the parameter in the request header Access-Control-Allow-Origin. In PHP, I believe it can be done with something like:

<?php    
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
  • This php is authorizing any source, right? If it is, it’s good to make it clear.

0

I had the same problem. I managed to solve including in php file:

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.