2017-09-05 46 views
0

我正在使用SLIM框架作爲後端和Angularjs作爲前端。正常的GET和POST API的工作正常。每當我在GET或POST方法中傳遞標頭時,它將引發404錯誤。我使用.htaccess文件的CORS。SLIM php Angularjs CORS

RewriteEngine On 

# Some hosts may require you to use the `RewriteBase` directive. 
# If you need to use the `RewriteBase` directive, it should be the 
# absolute physical path to the directory that contains this htaccess file. 
# 
# RewriteBase/

Header add Access-Control-Allow-Origin: "*" 
Header add Access-Control-Allow-Headers: "origin, x-requested-with, content-type, authorization" 
Header add Access-Control-Allow-Methods: "PUT, GET, POST, DELETE, OPTIONS" 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ index.php [QSA,L] 

請幫我解決這個問題。

+0

檢查您的服務器的錯誤日誌。哪些請求正在生成404響應? – Phil

+0

機會是,您的服務器沒有正確響應飛行前的'OPTIONS'請求。讓我看看我能否找到一個全面解決方案 – Phil

+0

好。我一直在嘗試這一個多星期。我無法得到它 – user3667960

回答

0

如果您使用Chrome,則可以在Developer Tools中打開網絡選項卡以查看失敗的請求。如果它與CORS有關,那麼它會告訴你爲什麼失敗。

404錯誤通常意味着無法找到端點,因此請仔細檢查Angular代碼是否正在請求您認爲應該是的URL。

+0

404可能是因爲沒有任何響應飛行前的'OPTIONS'請求正確。 Slim框架應該被配置來處理它們 – Phil

+1

Slim會自動爲你處理OPTIONS。 –