Enabling CORS in CakePHP app

前端 未结 2 2013
青春惊慌失措
青春惊慌失措 2021-02-08 14:42

I am trying to enable CORS for an API built in CakePHP so that all requests are accessible with the following in the AppController:

public function beforeFilter(         


        
2条回答
  •  我在风中等你
    2021-02-08 15:10

    Based on what I found out here: Sending correct JSON content type for CakePHP

    The correct way to return JSON in CakePHP is like so:

    $this->response->type('json');
    $this->response->body(json_encode(array('message'=>'Hello world!')));
    

    This is because the headers can be overridden and therefore the CORS doesn't work unless you do it the 'proper' way using the response object in Cake.

提交回复
热议问题