2017-07-31 116 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

相關問題