簡體   English   中英

Ajax請求在不同的瀏覽器上無法正常工作

[英]Ajax requests not working the same on different browsers

我已經使用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不適用於移動設備。 任何想法是什么問題?

不太確定為什么要在PHP中使用CORS才是誠實的。 在此答案中一樣將其放在.htaccess文件 還要檢查此答案 (與以前的問題相同,以獲取有用的見解)。

引用答案:

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

在我自己的Zend Framework 2項目中使用的2個.htaccess文件下面。 第一個位於應用程序根目錄中,用於/當我忘記正確設置Apache vhost時。 它將所有內容重定向到公用文件夾的index.php文件。

第二個文件是ZF2默認.htaccess文件。 奇跡般有效。 從幾年前嘗試CodeIgniter 2以來,我使用了相同的文件。

顯然,您應該將第一個文件中的庫重寫到/ 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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM