无法通过ajax在CakePhp数据库中保存数据后通过保存函数我自己的数组变量


Unable to save data in database through ajax in CakePhp after passing my own array variable in save function?

这是我对ctp页面的看法:

<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>

这是PostsController函数

class PostsController extends AppController
{
        public $name = 'Posts';
        public $helpers = array('Html', 'Form', 'Session');
        public $components  = array('RequestHandler');
public function ajaxAdd()
    {
        $this->autoRender=false;
          if($this->RequestHandler->isAjax()){
             Configure::write('debug', 0);
          }
            if(!empty($this->data)){
                $inputData = array();
                $inputData['Post']['title'] = $this->data['Post']['ajaxtitle'];
                $inputData['Post']['body'] = $this->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";
               }
            }
        }
}

与数组作为$inputData在保存,每当我点击提交按钮没有被保存在数据库中。

但是当我在保存函数中传递$this->data时,像id,创建和修改的列被填充,但标题和主体列留空。

My posts database包含id,title,body,created,modified

你可以试试下面的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->data中获得数组。但首先运行这段代码与echo "<pre>";print_r($this->data);在后控制器检查您是否获得数据

如果您使用的是CakePHP 3.0而不是更早的版本,您应该使用$this->request->data()而不是$this->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";
           }
        }
    }