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,16 +207,22 @@ class AjaxControllerEx extends Extension
$ctrl->config()->get('ajax_resources') $ctrl->config()->get('ajax_resources')
); );
$response->setBody(json_encode([ $body = $response->getBody();
'ID' => $record->ID,
'Title' => $record->Title, if (!self::isJson($body)) {
'Link' => $ctrl->Link(), $body = json_encode([
'CSSClass' => $ctrl->CSSClass(), 'ID' => $record->ID,
'Resources' => $resources, 'Title' => $record->Title,
'RequestLink' => $url, 'Link' => $ctrl->Link(),
'MainContent' => $ctrl->customise([ 'CSSClass' => $ctrl->CSSClass(),
'Layout' => DBHTMLText::create()->setValue($response->getBody()), 'Resources' => $resources,
])->renderWith('Includes/MainContent')->RAW(), 'RequestLink' => $url,
])); 'MainContent' => $ctrl->customise([
'Layout' => DBHTMLText::create()->setValue($response->getBody()),
])->renderWith('Includes/MainContent')->RAW(),
]);
}
$response->setBody($body);
} }
} }