I am using Joomla\CMS\Response\JsonResponse to send a JSON response.

$data= new \stdClass()
$data->some_data = 1;
$resp = new JsonResponse($data);
echo $resp;

How to set a HTTP header for the response?

  • I am noticing that you are using return instead of echo in your snippet, so I am wondering how you are using this technique. You will see in the documentation docs.joomla.org/JSON_Responses_with_JResponseJson that this class does more than just json_encode() data. Either way, you don't necessarily need to go to the trouble to declare an object because an array will do just fine. E.g. return new JsonResponse(['some_data' => 1]); Can you show us more of what you are trying to do? Adding context is a great way to add clarity to a question.
    – mickmackusa
    Apr 24 '20 at 15:41
  • I used the static method JResponse::setHeader(...) and it worked (but is deprecated)
    – ed22
    Apr 24 '20 at 16:06
  • Please edit your question so that the full story is in the question body. Thank you.
    – mickmackusa
    Apr 24 '20 at 16:08
  • could you please edit your question to show the code that you were using to create the json data and set the header?
    – mickmackusa
    Apr 25 '20 at 20:57

The recommended replacement according to the Joomla documentation:

Element                   Line        Description
\JResponse::setHeader()   83          3.2 Use JApplicationWeb::setHeader() instead


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.