我使用Codeigniter编写了我的简单Web应用程序游戏。
这是我感到困惑的地方。我正在使用Ajax请求能够转到我的不同路由,从位置按钮返回不同数量的黄金。我希望能够在后台连续播放音乐...因此这种方法。
问题是我在Chrome上收到此错误。
XMLHttpRequest cannot load http://www.travterrell.com/ninjagold/Ninjas/cave. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://travterrell.com' is therefore not allowed access.
当我最初发现此错误时,我已查找此问题并发现我需要将此代码插入travterrell.com的索引文件,因为Ninja Gold游戏是我个人网站上的子索引
<?php header('Access-Control-Allow-Origin: *'); ?>
我还被指示将我的.htaccess文件修改为:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /ninjagold
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
特别是第3行,我重新编写了基础。首先,这解决了问题,但现在我注意到游戏无法在Chrome上运行,有时可以在桌面上使用Safari。它适用于Safari移动设备,但单击按钮接收或丢失黄金时的声音效果不起作用。 Chrome无法在移动设备上运行。任何想法是什么问题?
答案 0 :(得分:0)
不太确定为什么要将PHP中的CORS置于荣誉之中。将其放在.htaccess
文件中,例如in this answer。同时检查this answer(与之前相同的问题,以获得有用的见解)。
引用回答:
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
在我自己的Zend Framework 2项目中使用的2 .htaccess
个文件之下。第一个,在应用程序根目录中,几乎是为了/当我忘记正确设置Apache vhost时。它将所有内容重定向到公用文件夹的index.php文件。
第二个文件是ZF2默认的.htaccess。奇迹般有效。从我尝试CodeIgniter 2(几年前)开始,我使用了相同的文件。
显然你应该将第一个文件中的base重写到你的/ ninjagold位置。但是,如果您的应用程序有一个/public
文件夹作为输入点,请将其重写为/ninjagold/public
。
root(/).htaccess
RewriteEngine On
# If URL to the application is http://foo.com/path/to/ZendSkeletonApplication/
# the set the base to /path/to/ZendSkeletonApplication/
RewriteBase /
# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Below is ZF2 default
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ public/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ public/index.php [NC,L]
<强> /public/.htaccess
强>
RewriteEngine On
# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
# The following rewrites all other queries to index.php. The
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size
# fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]