0

这是我的代码片段。

评论控制器

public function edit_comments()
{
    $this->autoRender = false;

    $data = json_decode($_POST['data'], true);

    // print_r(json_encode($this->request->data));

    if (empty($data['id']))
    {
        echo "Invalid Post";
    }

    echo $data['id'];

    $post = $this->Comment->viewById($data['id']);

    if (empty($post))
    {
        echo 'Invalid post';
    }

    if ($this->request->is(array('post', 'put'))) {
        $this->Comment->id = $data['id'];
        if ($this->Comment->save($this->request->data)) // I able to bypass this if
        {
            echo "Successfully Updated";
            exit;
        }

        echo "error";
    }
}

这是我的自定义 Jquery

$(document).ready(function(){

$('.edit').click(function(e){
    e.preventDefault();
    var comment = $(this).closest('td').prev().children('p.comment-text').text(),
        id = $(this).closest('td').prev().attr('data-id'),
        tag = $(this).closest('td').prev();

        console.log(comment);

        tag.find('textarea#comment').val(comment);
        tag.find('input#comment_id').val(id);
});

$('#update').on('click',function(e){
    e.preventDefault();

    var comment = $('#comment').val(),
        comment_id = $('#comment_id').val(),
        data = {

            comment: comment,
            id: comment_id

        };

    $.ajax({
        type: 'post',
        url: '/stuff/Blog/comments/edit_comments',
        data: {data: JSON.stringify(data)},
        dataType: 'JSON',
        success: function(response){
            console.log(response);
        }

    });

});

});

我的问题是我无法更新数据库中的数据,但我可以在 edit_comment 函数中打印所有数据。我可以使用保存功能在 if 中打印“更新成功”。

4

1 回答 1

0
data: {data: JSON.stringify(data)},

应该

data: JSON.stringify(data),

你可以在这里查看广泛的 jQuery ajax API

于 2015-12-20T15:32:27.057 回答