When you create a template file like my-ajax-handler.json.twig, Craft will automatically send a Content-Type: application/json header. No need for {% header ... %} tags in the template—just echo out the JSON!
This should work for any MIME type detectable via craft\helpers\FileHelper::getMimeTypeByExtension($template)—but please don't try and "render" an MP3 in Twig. :)
Product
Craft CMS
Description
When you create a template file like
my-ajax-handler.json.twig
, Craft will automatically send aContent-Type: application/json
header. No need for{% header ... %}
tags in the template—just echo out the JSON!This should work for any MIME type detectable via
craft\helpers\FileHelper::getMimeTypeByExtension($template)
—but please don't try and "render" an MP3 in Twig. :)