2017-07-31 113 views
0

我使用Codeigniter编写了简单的Web应用程序游戏。Ajax请求在不同浏览器上的工作方式不同

Ninja Gold

这里就是我很困惑。我正在使用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的索引​​文件,因为忍者黄金游戏是我的个人网站

子索引
<?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

不太确定为什么你会把CORS放在PHP里面。将它放在.htaccess文件中,如in this answer。还请检查this answer(与以前相同的问题,以获取有用的见解)。从答案

报价:

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

下面的2个.htaccess文件,我在我自己的Zend Framework 2项目中使用。第一个,在应用程序根目录下,非常适用于当我忘记正确设置Apache vhost时。它将所有内容重定向到公共文件夹的index.php文件。

第二个文件是ZF2默认的.htaccess。奇迹般有效。从我尝试CodeIgniter 2(几年前)开始,我使用了相同的文件。

显然你应该把第一个文件中的base重写到你的/ ninjagold位置。但是,如果您的应用程序有一个/public文件夹作为输入点,请将其重写为/ninjagold/public

根(/).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] 
+0

所以第一个.htaccess文件应该可以正常工作正确吗?我是CodeIgniter的新手,我试图在.htaccess文件中找到更多信息。 –

+0

这取决于你想要做什么,这有点不清楚。你说你是AJAX功能不能正常工作,但是只能谈谈你的'.htaccess'文件。这导致(我)认为你收到'500内部服务器错误'响应,或者'404找不到'。最有可能的是后者。但是,如果您链​​接到“website.com/ninjagold/get_me_data”之类的东西,那么您需要在我的项目根目录中的.htaccess第一个.htaccess,因为它不是您的网站根目录,可以将您从/ ninjagold/to/ninjagold/public ... – Nukeface

+0

除了我自己的知识,主要是基于ZF2的,并且发现你[this](https://stackoverflow.com/questions/40003236/ codeigniter-web-config-or-htaccess-index-php-rewrite-inside-a-subdirectory),似乎是你确切的问题;它是关于codeigniter应用程序中的codeigniter应用程序(实质上是子目录中的应用程序)。不知道下一个,但[它可能会有所帮助](https://serverfault.com/questions/820040/codeigniter-is-not-working-after-primary-domain-to-subfolder)。 – Nukeface

相关问题