Javascript 用CakePHP保存数据,同时通过AJAX发布JSON?
所以我有一个表单,我有一个KnockoutJs应用程序,有一个CakePHP后端。当我点击Cake的默认保存按钮时,我想吐出一个JSON并将其与标准表单数据一起发布 以下是我目前在JS中的内容:Javascript 用CakePHP保存数据,同时通过AJAX发布JSON?,javascript,ajax,json,cakephp,Javascript,Ajax,Json,Cakephp,所以我有一个表单,我有一个KnockoutJs应用程序,有一个CakePHP后端。当我点击Cake的默认保存按钮时,我想吐出一个JSON并将其与标准表单数据一起发布 以下是我目前在JS中的内容: $('input.saveProgram').click(function() { var theJson = ko.mapping.toJSON(pvm, mapping); $.ajax({ url: 'http://localhost/cake/programs/e
$('input.saveProgram').click(function() {
var theJson = ko.mapping.toJSON(pvm, mapping);
$.ajax({
url: 'http://localhost/cake/programs/edit',
dataType: 'json',
type: 'POST',
data: theJson
});
});
在Cake中,我尝试在我的控制器中使用请求处理程序,但没有效果:
if($this->RequestHandler->setContent('json', 'application/json')) {
// standard saving code
}
在我的Cake应用程序中,我尝试了die$this->request->data来查看发生了什么,而JSON似乎根本没有发布。在我解释您的问题时,这里有一个解决方案。在控制器中:
if($this->RequestHandler->isAjax()){
// "spit" out json
echo $this->data;
//decode data into an array
$decodedData = json_decode($this->data);
//standard saving code would
$this->Model->save($decodedData);
}