In following the documentation at https://docs.joomla.org/JSON_Responses_with_JResponseJson
Created a keyed array of data $api_result and echo that as a json response:
try {
echo new JResponseJson($api_result);
}
catch(Exception $e) {
echo new JResponseJson($e);
}
exit;
json string having in the response header the content-type set to "application/json".
json string with in the response header the content-type set to "text/html"
Forcing the content type using:
JFactory::getDocument()->setMimeEncoding( 'application/json' );
Does not work. It requires to set the content type by PHP. So:
This DOES NOT work:
try {
JFactory::getDocument()->setMimeEncoding( 'application/json' );
echo new JResponseJson($api_result);
}
catch(Exception $e) {
echo new JResponseJson($e);
}
exit;
And this DOES work:
try {
header('Content-Type: application/json');
echo new JResponseJson($api_result);
}
catch(Exception $e) {
echo new JResponseJson($e);
}
exit;
So, the json string is build and displayed, however, the content-type not set to application/json.
and
try {
JFactory::getDocument()->setMimeEncoding( 'application/json' );
echo new JResponseJson($api_result);
}
catch(Exception $e) {
echo new JResponseJson($e);
}
exit;
why this should work if you use exit() that Interrupt app execution?
I missed the query, I assumed that the function would force the Content-Type due to its name.
The exit actually does work.
However, when I use &format=json it still does not work. Though I am used to the &format=... for raw and own types as well.
One thing is sure, I instantiate a controller and perform the execute with its task. In that controller I don't perform any parent method. Also not parent::display() as I remember that is necessary for triggering the format request. If I add parent::display(), still does not work.
I might be following a different flow than required for this, not sure. If so, it might be the way I catch the task from the request which I need for now. This is my controller instantiation in the entry file:
require_once JPATH_COMPONENT.DS.'controllers'.DS.'postprocessor.php';
$controller = new myapplicationControllerPostprocessor();
$controller->execute($remote_task);
$controller->redirect();
In this exectute I echo the jsonresponse.
Nevertheless, the issue might very likely be at my side for I overlooked the format request.
I tried this:
JFactory::getApplication()->input->set('format', "json");
try {
echo new JResponseJson($api_result);
}
catch(Exception $e) {
echo new JResponseJson($e);
}
// And no exit
The result is that my site loads with json in the component jdoc field.
Even when I set JFactory::getApplication()->input->set('format', "json"); in the entry file, no success.
Refactoring controller instantiation to:
$controller = JControllerLegacy::getInstance('myapplication');
$input = JFactory::getApplication()->input;
$controller->execute($input->getCmd('task'));
$controller->redirect();
And calling the cms with:
index.php?option=com_myapplication&task=postprocessor.getjob&format=json
performing the task in the controller execute and do the jsonresponse as follows:
try {
echo new JResponseJson($api_result);
}
catch(Exception $e) {
echo new JResponseJson($e);
}
// No exit, exit in comment!
//exit;
Sets the Content-Type to text/plain.
You're not sending the headers if you call exit, that's why you're missing a lot of stuff there.
As for the text/plain
content type, the JsonDocument constructor only sets the content type to application/json
if the request sends the appropriate Accept header.
JResponseJson
is not an all knowing response object, it's an encapsulation of the response body. The response headers are still at the application/document level.
@andriesbron your last comment should work
@Fedik @mbabker It works indeed, you knew of course. Somehow (meaning I don't know what I did) my last comment indeed produces json rather than text/plain. What I did do is activate the Accept header to json, tried it, it worked, then deactivated it, tried it and it still worked. That puzzles me, but consider it to be my less structured approach in trying to solve it.
The json is however send as attachment rather than inline, I prefer the latter.
Thanks for your help, it gave me new insights.
the issue can be closed
Status | New | ⇒ | Expected Behaviour |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2020-04-23 12:20:30 |
Closed_By | ⇒ | SharkyKZ |
Status | Expected Behaviour | ⇒ | Closed |
Closed_By | SharkyKZ | ⇒ | joomla-cms-bot |
Set to "closed" on behalf of @SharkyKZ by The JTracker Application at issues.joomla.org/joomla-cms/28762
Closed as expected behaviour.
the request should be with
&format=json