简体   繁体   中英

Set headers in zf2

i have set up a following headers in my controller, with the following code

header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST');
    header("Access-Control-Allow-Headers: X-Requested-With, Content-Type");

how can i achieve this in the zend framework 2,

Thanks

use Zend\Http\Headers;

...

$headers = new Headers();

or

$headers = $httpObject->getHeaders();

then to add headers one by one

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

or pass all in one array like this

$headers->addHeaders(array(
    'Access-Control-Allow-Origin' => '*',
    'Access-Control-Allow-Methods' => 'GET, POST',
    'Access-Control-Allow-Headers' => 'X-Requested-With, Content-Type'
));

if you created a new headers object

$httpObject->setHeaders($headers);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM