2017-02-04 110 views
0

am buyildiung一個angular2應用程序並託管php共享主機上的後端。 每當我發送POST請求我得到的在htaccess中啓用apache headers_module

No 'Access-Control-Allow-Origin' header is present on the requested resource 

錯誤在我的本地XAMPP我不得不啓用headers_module在Apache來解決這個,我已經聯繫了共享的託管公司,但他們不能使模塊

我該如何去解決這個可能是在.htaccess文件

這是當前.htaccess文件

RewriteEngine on 

# hide files and folders 
RedirectMatch 404 /_protected 
    RedirectMatch 404 /\.git 
RedirectMatch 404 /composer\. 
RedirectMatch 404 /.bowerrc 

# If a directory or a file exists, use the request directly 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
# Otherwise forward the request to index.php 
RewriteRule . index.php 
Header set Access-Control-Allow-Origin "*" 
Header set Access-Control-Allow-Headers "Content-Type" 

w ^帽子我是否需要在上面的.htaccess文件添加到作爲一個四處爲這個錯誤,甚至作爲旁路的

headers_module 

回答

0

不,你不能使用.htaccess加載的模塊。該LoadModule directive背景是:

server config, virtual host 

但你並不需要依賴於服務器的配置,PHP可以生成HTTP頭就好了:

header('Access-Control-Allow-Origin: *'); 

reference

當然, Yii有一個builtin mechanism to generate HTTP headers

+0

我也在yii2中設置了標題,但仍然返回錯誤,請求的資源上沒有'Access-Control-Allow-Origin'標題 –