Jquery .load () cross origin from the same subdomain

1

I have a problem when updating the content of a section when I try to do it with Jload's .load ().

I do not know why but it tries to load the content of the main domain instead of the subdomain in which I am.

The error I receive is:

  

XMLHttpRequest can not load link . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' link ' is therefore not allowed access.

The line I use is:

$('.refresh').load('contact.php?id=' + contactId + ' .refresh');
    
asked by Infobuscador 08.03.2017 в 18:10
source

1 answer

5

Your problem is because " link " has a subdomain different from " link " even if the domain is the same the 'test' subdomino is different from 'www', if you want it to work you must add the CORS headers to your contact.php file.

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");

Unfortunately this gives access to any domain

Another solution would be

$http_origin = $_SERVER['HTTP_ORIGIN'];
if ($http_origin == "https://www.miweb.com" || $http_origin == "http://www.domain2.com" || $http_origin == "https://prueba.miweb.com")
{  
    header("Access-Control-Allow-Origin: $http_origin");
}

Finally you could modify your .htaccess file if you use apache. To make something like this.

<FilesMatch "\.(ttf|otf|eot|woff)$">
    <IfModule mod_headers.c>
        SetEnvIf Origin "http(s)?://(prueba.miweb.com|www.miweb.com)$" AccessControlAllowOrigin=$0
        Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
    </IfModule>
</FilesMatch>

EDIT: Or you can simply put everything under the same domain www.miprueba.com and you avoid setting headers.

    
answered by 08.03.2017 / 18:27
source