FIX: Json response

This commit is contained in:
Tony Air 2024-03-11 17:39:27 +02:00
parent abcca72aa3
commit 170afbaa73

View File

@ -78,7 +78,7 @@ class AjaxControllerEx extends Extension
//$form->addExtraClass('ajax-form'); //$form->addExtraClass('ajax-form');
if ($form->get_protector()) { if (Director::isLive() && $form->get_protector()) {
$form->enableSpamProtection(); $form->enableSpamProtection();
} }
@ -113,7 +113,7 @@ class AjaxControllerEx extends Extension
) )
); );
if ($form->get_protector()) { if (Director::isLive() && $form->get_protector()) {
$form->enableSpamProtection(); $form->enableSpamProtection();
} }
@ -186,6 +186,12 @@ class AjaxControllerEx extends Extension
return SSViewer::create($tpl); return SSViewer::create($tpl);
} }
private static function isJson($str)
{
$json = json_decode($str);
return $json && $str != $json;
}
public function prepareAjaxResponse($response) public function prepareAjaxResponse($response)
{ {
$ctrl = $this->owner; $ctrl = $this->owner;
@ -201,7 +207,10 @@ class AjaxControllerEx extends Extension
$ctrl->config()->get('ajax_resources') $ctrl->config()->get('ajax_resources')
); );
$response->setBody(json_encode([ $body = $response->getBody();
if (!self::isJson($body)) {
$body = json_encode([
'ID' => $record->ID, 'ID' => $record->ID,
'Title' => $record->Title, 'Title' => $record->Title,
'Link' => $ctrl->Link(), 'Link' => $ctrl->Link(),
@ -211,6 +220,9 @@ class AjaxControllerEx extends Extension
'MainContent' => $ctrl->customise([ 'MainContent' => $ctrl->customise([
'Layout' => DBHTMLText::create()->setValue($response->getBody()), 'Layout' => DBHTMLText::create()->setValue($response->getBody()),
])->renderWith('Includes/MainContent')->RAW(), ])->renderWith('Includes/MainContent')->RAW(),
])); ]);
}
$response->setBody($body);
} }
} }