簡體   English   中英

通過AJAX和mysql表400的Yii2 like按鈕錯誤錯誤請求

[英]Yii2 like button via AJAX and mysql table 400 error bad request

我達到了這種狀態:我做了一張桌子->

| likes |
| like_id(primary key) | user_id(foreign key) | comment_id(foreign key) |

單擊“喜歡”鏈接后,我的代碼應該在“ likes表中添加一行,並刷新保存了“喜歡”計數的<div id="like_"<?php $comment['comment_id'] ?>> 這應該通過ajax發生。 我對AJAX不熟悉,所以請給我建議。 我走對了嗎?

我的行動反饋:

public function actionFeedback($comment_id)
{
    if(\Yii::$app->request->isAjax)
    {
        $user_id = \Yii::$app->user->identity->id;

        \Yii::$app->db->createCommand("INSERT INTO likes(user_id, comment_id)
                                            VALUES ($user_id, $comment_id)")->execute();

        $likes = \Yii::$app->db->createCommand("SELECT COUNT(*)
                                                     FROM likes
                                                     WHERE comment_id=$comment_id")->queryScalar();

        return $likes;
    }

}

和我的看法comments.php:

<?php

use yii\helpers\Html;
use app\models\BlogUser;
use app\controllers\PostController;
?>
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <?php foreach ($comments as $comment): ?>
                <div class='col-md-3 post-prof-img'>
                    <?php
                        $currUser = BlogUser::find()->where(['id' => $comment['author_id']])->one();

                        $commentID = $comment['comment_id'];

                        $likes = \Yii::$app->db->createCommand("SELECT COUNT(*)
                                                                    FROM likes
                                                                    WHERE comment_id=$commentID")->queryScalar();
                    ?>
                    <?= Html::img('../images/' . $currUser->image, ['class' => 'tall img-circle']) ?>
                    <p class="text-center title-par">
                        <em><strong><?= $currUser->username ?></strong></em>
                    </p>
                </div>
                <div class='col-md-9 col-md-offset-1'>
                    <div class='post-comment'>
                        <p><em><?= $comment['comment_content'] ?></em></p>
                    </div>
                    <div class='comment-options'>
                        <div class='col-md-8'>
                         <?php
                             if(\Yii::$app->user->identity->id == $comment['author_id'] || PostController::isAdmin())
                             {
                                 echo Html::a('Edit',['update-comment', 'id' => $comment['comment_id']]);
                                 echo Html::a('Delete',
                                     ['delete-comment', 'id' => $comment['comment_id']],
                                     ['data' => [
                                                 'confirm' => 'Are you sure?',
                                                 'method' => 'POST'
                                                ]
                                     ]);
                             }
                             echo Html::a('Like',[''],['class' => 'like_up', 'data_id' => $comment['comment_id']]);
                             echo Html::a('Dislike');
                          ?>
                        </div>
                        <div class='col-md-4 text-right'>
                            <div class="ajax-helper">
                                <span class='glyphicon glyphicon-hand-up' style="color:green"></span>
                                <span id="likes-"<?php $comment['comment_id'] ?>><?= $likes ?></span>
                                <span class='glyphicon glyphicon-hand-down' style="color:red"></span>

                            </div>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
            <?php
                $this->registerJs("
                    $('.like_up').on('click', function(event){
                        event.preventDefault();
                        var id = $(this).attr('data_id');
                        $.ajax({
                            url : '".\yii\helpers\Url::to(['feedback'])."?id='+id,
                            cache : false,
                            success : function( data ){
                                $('#likes-'+id).html( data );
                            }
                        });
                    });
                ");
            ?>
            <div>
                <?= Html::a('Add Comment', ['create-comment', 'id' => $_GET['id']],['class' => 'btn btn-primary add-comment', 'style'=>'margin-top: 2%']) ?>
            </div>
        </div>
    </div>
</div>

有了這行代碼,我得到一個錯誤: GET http://letsblog/post/feedback?id=25&_=1491990823748 400 (Bad Request)從我在Google上發現的問題出在我的Ajax部分,但不知道如何解決://預先謝謝!

url : '".\\yii\\helpers\\Url::to(['feedback'])."?comment_id='+id,將url替換為ajax請求部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM