2015-11-19 89 views
2

我在CakePHP 1.3中創建了一個JSON webservice。CakePHP 1.3上的CORS

我希望能夠將來自不同服務器的AJAX請求發送到此Web服務。

我知道這是Cake 2+中的一個小插曲,但我不能爲我的生活找出v1.3。 Based on what I have found in the docs,我試圖在我的控制器如下:

public $components = array('RequestHandler'); 

function beforeFilter() { 
    Configure::write('debug', 0); 
    $this->header('Access-Control-Allow-Origin','*'); 
    $this->header('Access-Control-Allow-Methods','*'); 
    $this->header('Access-Control-Allow-Headers','X-Requested-With'); 
    $this->header('Access-Control-Allow-Headers','Content-Type, x-xsrf-token'); 
    $this->header('Access-Control-Max-Age','172800'); 
} 

這不幸導致500 ERR_INVALID_RESPONSE

我試圖做$this->response->header而不是$this->header,和同樣的問題。我也試過header而不是$this->header,並且我得到的服務器響應爲500。我也有嘗試將header塊移動到我在控制器中的動作,進入頂​​層app_controller.php文件,進入bootstrap.php文件,並進入視圖文件本身(每個變體header,$this->header和嘗試了$this->response->header)。刪除Configure::write('debug', 0);不能解決問題。

哎呀,我連這個在我.htaccess文件:

<IfModule mod_headers.c> 
    <FilesMatch "\.(json)$"> 
     Header set Access-Control-Allow-Origin "*" 
    </FilesMatch> 
</IfModule> 

雖然這似乎並沒有做任何事情。我在Apache的mods-available文件夾中也有mod_headers.load

任何人都知道如何在CakePHP 1.3中設置Access-Control-Allow-Origin標頭?

回答

0
header("Access-Control-Allow-Origin: *"); 

我知道這是一箇舊帖子,但我只是想回答這個以幫助他人。