Ich versuche, den Content-Type-Header für eine JSON-Antwort festzulegen, auf die mit einer AJAX GET-Anfrage zugegriffen wird. Ich habe Tutorials auf Blogs und in der Bäckerei verfolgt, bekomme aber immer 'text/html' von CakePHP zurück. Wie setze ich den Content-Type-Header richtig?Wie wird der korrekte Inhaltstyp für JSON in CakePHP zurückgegeben?
Hier ist mein Code im Moment:
public function admin_controller_actions_ajax()
{
Configure::write('debug', 0);
if ($this->RequestHandler->isGet()) {
$this->autoRender = FALSE;
$aco_id = $this->params['url']['aco_id'];
$aro_id = $this->params['url']['aro_id'];
assert('$aco_id != NULL && $aro_id != NULL &&
is_numeric($aco_id) && is_numeric($aro_id)');
$actions = $this->Resource->getActionsForController($aco_id, $aro_id);
assert('is_array($actions) && is_array($actions[0])');
/* I made RequestHandler part of the $components property */
$this->RequestHandler->setContent('json');
$this->RequestHandler->respondAs('json'); /* I've tried 'json', 'JSON', 'application/json' but none of them work */
$this->set('json_content', json_encode(array('response' => $actions[0])));
$this->layout = NULL;
$this->render('/json/default');
}
}
/* json/default.ctp */
<?php echo $json_content; ?>
Jede Hilfe würde geschätzt.
Danke,
- Isaac
Verwenden Sie Router :: parseExtensions() in Ihrer Datei routes.php? Wenn ja, müssen Sie "debug" in app/config/core.php auf 0 setzen, damit es mit dem richtigen Inhaltstyp antwortet. – dhofstet
Nein, ich verwende Router :: parseExtensions() nicht, aber ich setze Debug in der Anfrage trotzdem auf 0. –