这是我的观点ctp页面:
var editing: Bool = false {
didSet {
if !self.editing {
self.barSlider.addPanRecognizer()
// Removed code, to keep this example short...
} else {
self.barSlider.removePanRecognizer()
// Removed code, to keep this example short...
}
}
}
这是我的PostsController功能
<h1>Add Post</h1>
<?php echo $this->form->create(null,array('url'=>array('controller'=>'posts','action'=>'ajaxAdd'),'id'=>'saveForm'));
echo $this->form->input('ajaxtitle');
echo $this->form->input('ajaxbody',array('rows'=>'3'));
echo $this->form->end('Save Post');
?>
<script>
$(document).ready(function(){
$("#saveForm").submit(function(){
var formData = $(this).serialize();
var formUrl = $(this).attr('action');
$.ajax({
type:'POST',
url:formUrl,
data:formData,
success: function(data,textStatus,xhr){
alert(data);
}
});
return false;
});
});
</script>
在保存中使用数组作为 $ inputData ,每当我点击提交按钮时,数据库中都没有保存任何内容。
但是当我在保存功能中传递 $ this-&gt;数据时,会填充ID,已创建和修改的列,但标题和正文列会留空。
我的帖子数据库包含 id,title,body,created,modified 列。
答案 0 :(得分:0)
您可以在javascript中尝试下面的代码
$("#saveForm").submit(function(){
var formData = $(this).serializeArray();
var formUrl = $(this).attr('action');
$.ajax({
type:'POST',
url:formUrl,
data:formData,
success: function(data,textStatus,xhr){
alert(data);
}
});
您没有在ajax请求中发送数组,这就是为什么您没有在$ this-&gt;数据中获取数组。但首先在帖子控制器中使用echo "<pre>";print_r($this->data);
运行此代码,以检查您是否正在获取数据
答案 1 :(得分:0)
如果您使用的是CakePHP 3.0而不是早期版本,则应使用$ this-&gt; request-&gt; data()而不是$ this-&gt; data。
http://book.cakephp.org/3.0/en/controllers/request-response.html#request-body-data
如果是这种情况,代码应如下:
public function ajaxAdd()
{
$this->autoRender=false;
if($this->RequestHandler->isAjax()){
Configure::write('debug', 0);
}
if(!empty($this->request->data())){
$inputData = array();
$inputData['Post']['title'] = $this->request->data('Post.ajaxtitle');
$inputData['Post']['body'] = $this->request->data('Post.ajaxbody');
$data = $this->Post->findByTitle($inputData['Post']['title']);
$this->Post->create();
if(empty($data))
{
if($this->Post->save($inputData))
return "success";
}else
{
return "error";
}
}
}