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?
The recommended replacement according to the Joomla documentation:
Element Line Description \JResponse::setHeader() 83 3.2 Use JApplicationWeb::setHeader() instead
return
instead ofecho
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 justjson_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.