[英]Saving data in CakePHP, and posting JSON via AJAX simultaneously?
So I have a form, AND I have a KnockoutJs app, with a CakePHP back end.所以我有一个表单,我有一个 KnockoutJs 应用程序,后端为 CakePHP。 When I hit Cake's default "save" button, I'm wanting to spit out and post a JSON along with the standard form data.
当我点击 Cake 的默认“保存”按钮时,我想吐出并发布一个 JSON 以及标准表单数据。
Here's what I have in my JS so far:到目前为止,这是我的 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
});
});
In Cake, I'm trying to use the the Request handler in my controller, but to no avail:在 Cake 中,我试图在我的 controller 中使用请求处理程序,但无济于事:
if($this->RequestHandler->setContent('json', 'application/json')) {
// standard saving code
}
In my Cake app I've tried die($this->request->data) to see what's going on and the JSON doesn't seem to be posting at all.在我的 Cake 应用程序中,我尝试使用 die($this->request->data) 查看发生了什么,而 JSON 似乎根本没有发布。
Here's a solution as I interpret your question.这是我解释您的问题时的解决方案。 In your controller:
在您的 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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.