我使用codeigniter 4在文档中说使用setHeader
函数(从控制器内)
例如:
$this->response->setHeader('Content-Type', "application/pdf");
在上面的例子中我仍然得到'Content-Type': 'text/html'
当我看到chrome开发工具的响应。
但是,如果我使用header()
函数,即:
header("Content-Type: application/pdf");
我得到了预期的结果。
为什么会这样?
Try $this->response->setContentType("application/pdf");我想这就是它在codeigniter中改变内容类型的原因。
您必须链接send方法。如果没有send方法,它将不会改变报头。
下面是如何使用它的代码片段:
$this->response->setContentType('application/json')->send();