htaccess Access-Control-Allow-Origin


Estoy creando un script que se carga externamente en otros sitios. Carga CSS y HTML y funciona bien en mis propios servidores.

Sin embargo, cuando lo pruebo en otro sitio web, muestra este terrible error:

Access-Control-Allow-Origin

Aquí puedes ver que se carga perfectamente: http://tzook.info/bot /

Pero en este otro sitio web muestra el error: http://cantloseweight.co/robot /

Subí el script de carga a jsfiddle: http://jsfiddle.net/TL5LK /

He intentado editar el archivo htaccess así:

<IfModule mod_headers.c>    
    Header set Access-Control-Allow-Origin *
</IfModule>

O así:

Header set Access-Control-Allow-Origin *

Pero todavía no funciona.

Author: Nate, 2012-11-16

8 answers

Pruebe esto en el .htaccess de la carpeta raíz externa:

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
</IfModule>

Y si solo concierne .scripts js debe envolver el código anterior dentro de esto:

<FilesMatch "\.(js)$">
...
</FilesMatch>
 181
Author: vifargent,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-12-14 00:40:09

Nadie dice que usted también tiene que tener mod_headers habilitado, así que si todavía no funciona, intente esto:

(los siguientes consejos funcionan en Ubuntu, no sé sobre otras distribuciones)

Puede comprobar la lista de módulos cargados con

apache2ctl -M

Para habilitar mod_headers puede usar

a2enmod headers

Por supuesto después de cualquier cambio en Apache tienes que reiniciarlo:

/etc/init.d/apache2 restart

Entonces puedes usar

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
</IfModule>

Y si mod_headers no está activo, esta línea no hará nada en todo. Puede probar la cláusula skip if y simplemente agregar Header set Access-Control-Allow-Origin "*" en su configuración, entonces debería lanzar un error durante el inicio si mod_headers no está activo.

 34
Author: Lukas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-02 15:22:06

Desde mi experiencia;

Si no funciona desde dentro php hacer esto en .htaccess funcionó para mí

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin http://www.vknyvz.com  
    Header set Access-Control-Allow-Credentials true
</IfModule>
  • las credenciales pueden ser verdaderas o falsas dependiendo de sus parámetros de solicitud ajax
 24
Author: vknyvz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-06-14 15:06:24

Agregue un archivo .htaccess con las siguientes directivas a su carpeta fonts, si tiene problemas para acceder a sus fuentes. Se puede modificar fácilmente para su uso con .css o .archivos js.

<FilesMatch "\.(eot|ttf|otf|woff)">
    Header set Access-Control-Allow-Origin "*"
</FilesMatch>
 8
Author: Thoman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-07 03:21:08

En Zend Framework 2.0 tuve este problema. Se puede resolver de dos maneras .htaccess o cabecera php prefiero .htaccess así que modifiqué .htaccess de:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

A

RewriteEngine On

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
</IfModule>

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Y empieza a funcionar

 7
Author: YumYumYum,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-13 05:40:39

Las otras respuestas no funcionaron para mí, esto es lo que terminó haciendo el truco para apache2:

1) Habilitar los encabezados mod:

sudo a2enmod headers

2) Crea el archivo /etc/apache2/mods-enabled/headers.conf e inserta:

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
</IfModule>

3) Reinicie su servidor:

sudo service apache2 restart

 6
Author: shongololo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-04-07 08:01:04

POR cierto: el .la configuración de htaccess debe realizarse en el servidor que aloja la API. Por ejemplo, crea una aplicación AngularJS en x.com dominio y crear una API Rest en y.com, debe establecer Access-Control-Allow-Origin "*" en el .archivo htaccess en la carpeta raíz de y.com no x.com :)

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
</IfModule>

También como Lukas mencionó, asegúrese de haber habilitado mod_headers si usa Apache

 3
Author: Saman Shafigh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-27 22:43:02

Asegúrese de no tener una redirección sucediendo. Esto puede suceder si no incluye la barra final en la URL.

Vea esta respuesta para más detalles – https://stackoverflow.com/a/27872891/614524

 1
Author: JDavis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:26:38