Problem when trying to load an external page on my website

0

I would like to know how I can correct the following error:

  

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

When I try to load a page by example as follows

var shops = $('.link_shop');
    shops.on('click',function(e){
        e.preventDefault();
        var rute = $(this).attr('href');
        console.log(rute);
        $('#container_shops').load(rute);
});

What could be the error?

    
asked by Jhon Dember Murillo Mendez 06.04.2018 в 16:14
source

1 answer

3

You have a security problem, to solve it, you just have to tell the server that you want to pass certain types of requests.

You indicate that you have a web page (a domain) where you want to load the content of another (another different domain), then what you have to do is tell the server where the item to load is, that the destination domain can do what. You can do it with a .htaccess file on the remote server with the following content:

Access-Control-Allow-Origin: http://foo.example

Being foo.example the domain you want to let load the content. You can put * to let everyone (including your localhost).

You can see a full explanation of all the possibilities with examples in link

If you can not use a .htaccess or want more control, you can do it in php, an example taken from link published by Nikolay Ivanov would be:

$http_origin = $_SERVER['HTTP_ORIGIN'];
if ($http_origin == "http://www.domain1.com" || $http_origin == "http://www.domain2.com" || $http_origin == "http://www.domain3.info") {  
    header("Access-Control-Allow-Origin: $http_origin");
}
    
answered by 06.04.2018 в 16:29