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 に答える