Viktor Hesselbom
« Go back homeMagento: Override headers to not get duplicate Content-types
Today I needed to return a JSON response in a controller from my module in Magento. It need to have Content-type set to application/json in order for the Ajax library, namely prototype.js, to parse it as a JSON object.
The default Content-type in Magento is text/html. To change this you need to set a header in the response, so I did like this:
$this->getResponse()->setHeader('Content-type', 'application/json');What this did was to add another header instead of replacing it. So now I have two Content-type headers. This worked fine in Chrome because it would read both Content-types in the javascript but in Firefox it would only read the first which would still be text/html.
To fix this, all you need to do is to set the third parameter in the setHeader method, $replace, to true. It's quite self-explanatory but what this does is replace the previous header with the new value:
$this->getResponse()->setHeader('Content-type', 'application/json', true);It now returns just one Content-type header correctly set to application/json