Я имею Аякс после вызова к контроллеру CakePHP:Cakephp3: Как я могу вернуть данные json?
$.ajax({
type: "POST",
url: 'locations/add',
data: {
abbreviation: $(jqInputs[0]).val(),
description: $(jqInputs[1]).val()
},
success: function (response) {
if(response.status === "success") {
// do something with response.message or whatever other data on success
console.log('success');
} else if(response.status === "error") {
// do something with response.message or whatever other data on error
console.log('error');
}
}
});
Когда я пытаюсь это, я получаю следующее сообщение об ошибке:
действие контроллера может возвращать только торт \ Network \ Response или нуль ,
В AppController я это
$this->loadComponent('RequestHandler');
включен.
функция контроллера выглядит следующим образом:
public function add()
{
$this->autoRender = false; // avoid to render view
$location = $this->Locations->newEntity();
if ($this->request->is('post')) {
$location = $this->Locations->patchEntity($location, $this->request->data);
if ($this->Locations->save($location)) {
//$this->Flash->success(__('The location has been saved.'));
//return $this->redirect(['action' => 'index']);
return json_encode(array('result' => 'success'));
} else {
//$this->Flash->error(__('The location could not be saved. Please, try again.'));
return json_encode(array('result' => 'error'));
}
}
$this->set(compact('location'));
$this->set('_serialize', ['location']);
}
Что мне не хватает здесь? Нужны ли дополнительные настройки?
'Co Действия ntroller могут только возвращать Cake \ Network \ Response или null. 'Что так непонятно об этом сообщении об ошибке? Очевидно, вы возвращаете строку 'return json_encode()'. – burzum
Извините, я до сих пор не понимаю смысла? Я возвращаю массив, как в примере выше? – user1555112
У вас нет. http://php.net/manual/en/function.json-encode.php И вы когда-нибудь читали это? http://book.cakephp.org/3.0/ru/views/json-and-xml-views.html – burzum