【发布时间】:2013-02-03 10:07:20
【问题描述】:
我正在尝试为 CakePHP 中内置的 API 启用 CORS,以便通过 AppController 中的以下内容访问所有请求:
public function beforeFilter()
{
header("Access-Control-Allow-Origin: *");
}
这是在错误的地方吗?由于请求仍被阻止。
更新:看起来这确实有效,但是因为我正在做类似的事情:
header('Content-Type: application/json');
echo json_encode(array('message'=>'Hello world!'));
在我的一些方法中,它的行为好像覆盖了头设置 AppController,因此它不会出现在 JSON 调用的响应中。 有什么想法吗?
更新 2:返回 JSON 如下所示,修复了问题:
$this->response->type('json');
$this->response->body(json_encode(array('message'=>'Hello world!')));
那么显然在 Cake 中使用 header() 会破坏之前的标题?
【问题讨论】: