Yii框架无法设置Content-Type之谜

按照正常逻辑,直接一个函数搞定:

header('Content-Type: application/javascript');

在Yii框架中偏偏不好使,看了下Response源码发现:

public $format = self::FORMAT_HTML;

改变’format’属性为raw即可:

$response = \Yii::$app->response;
$response->format = Response::FORMAT_RAW;
$response->headers->add('Content-Type', 'application/javascript');