SLIM php Angularjs CORS

问题描述:

我正在使用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

如果您使用Chrome,则可以在Developer Tools中打开网络选项卡以查看失败的请求。如果它与CORS有关,那么它会告诉你为什么失败。

404错误通常意味着无法找到端点,因此请仔细检查Angular代码是否正在请求您认为应该是的URL。

+0

404可能是因为没有任何响应飞行前的'OPTIONS'请求正确。 Slim框架应该被配置来处理它们 – Phil

+1

Slim会自动为你处理OPTIONS。 –