我在控制器中设置了以下标题,代码如下
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With, Content-Type");
我如何在zend框架2中实现这一目标,
由于
答案 0 :(得分:2)
use Zend\Http\Headers;
...
$headers = new Headers();
或
$headers = $httpObject->getHeaders();
然后逐个添加标题
$headers->addHeaderLine('Access-Control-Allow-Origin', '*');
$headers->addHeaderLine('Access-Control-Allow-Methods', 'GET, POST');
$headers->addHeaderLine('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
或传递所有像这样的数组
$headers->addHeaders(array(
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => 'GET, POST',
'Access-Control-Allow-Headers' => 'X-Requested-With, Content-Type'
));
如果您创建了新的标题对象
$httpObject->setHeaders($headers);