所以我有一个表单,还有一个带有 CakePHP 后端的 KnockoutJs 应用程序。当我点击 Cake 的默认“保存”按钮时,我想吐出并发布 JSON 以及标准表单数据。
这是到目前为止我的 JS 中的内容:
$('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 中,我尝试在 Controller 中使用请求处理程序,但无济于事:
if($this->RequestHandler->setContent('json', 'application/json')) {
// standard saving code
}
在我的 Cake 应用程序中,我尝试使用 die($this->request->data) 来查看发生了什么,但 JSON 似乎根本没有发布。
最佳答案
这是我解释您的问题时的解决方案。在你的 Controller 中:
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);
}
关于javascript - 在 CakePHP 中保存数据,并同时通过 AJAX 发布 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10004609/