duannao1920 2015-11-19 23:51
浏览 76

CakePHP 1.3上的CORS

I'm creating a JSON webservice in CakePHP 1.3.

I want to be able to send AJAX requests from a different server to this webservice.

I know this is a cinch in Cake 2+, but I cannot for the life of me figure it out for v1.3. Based on what I have found in the docs, I have attempted the following in my controller:

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');
}

This unfortunately results in 500 ERR_INVALID_RESPONSE.

I have attempted to do $this->response->header instead of $this->header, and same problem. I've also attempted header instead of $this->header, and again I'm getting a server response of 500. I have also attempted moving the header block into my action in the controller, into the top-level app_controller.php file, into the bootstrap.php file, and into the view file itself (with every variation of header, $this->header, and $this->response->header attempted). Removing Configure::write('debug', 0); does not solve the issue.

Heck, I even have this in my .htaccess file:

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

Although that doesn't seem to do anything. I have mod_headers.load in my mods-available folder in Apache as well.

Anyone know how to set the Access-Control-Allow-Origin header in CakePHP 1.3?

  • 写回答

2条回答 默认 最新

  • dourao1877 2017-03-29 12:48
    关注
    header("Access-Control-Allow-Origin: *");
    

    I know this is an old post but I jut want to answer this to help others.

    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源