Laravel 5.8:自动刷新div在使用ajax时变得混乱和滞后



我有这个网站,每个新闻都有评论部分。我想使用 ajax 函数每 x 秒更新一次div。但是当我将 ajax 代码放入脚本中时,div 变得混乱,我的站点变得滞后,并且控制台中有很多错误。有人对此有想法吗?以下是我的输出:

在我实现代码之前:https://i.stack.imgur.com/AHKJF.jpg之后:https://i.stack.imgur.com/AE7Dj.jpg

秀讯.刀锋.php

Comments area
     <h4 class="comments-title" > <span class="fas fa-comment-alt"></span>
                    {{$news->comments()->count()}}
                    Comments</h4>
                  <div class="row" >
                      <div class="col-md-12 col-md-offset-2" style="overflow-y: scroll; height: 400px;
                      width: 400px; " id="commentarea" >
                          @foreach($news->comments as $comment)
                            <div class="comment" style="background-color: #f6efef;" >
                          <div class="author-info">
                              <img src={{"https://www.gravatar.com/avatar/" . md5(strtolower(trim($comment->email))) . "?s=50&d=retro" }} class="author-image" id="image">
                              <div class="author-name">
                                   <h4>{{$comment->name}} </h4>
                                   <p class="author-time"> {{  date('jS F, Y - g:iA' ,strtotime($comment->created_at)) }}</p>
                              </div>
                          </div>
                            <div class="comment-content">
                                    {{$comment->comment}}
                            </div>
                            </div>
                          @endforeach
                      </div>
                  </div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
      <script>
            $(document).ready(function() {
             setInterval(function() {
                $('#commentarea').load('{{ action('NewsController@showNews', ['news' => $news->id]) }}');
             }, 5000);
            });
           </script>

新闻控制器.php

<?php
namespace AppHttpControllers;
use DB;
use IlluminateHttpRequest;
use AppNews;
use Validator;
use Image;
use View;
use Storage;
use IlluminateSupportFacadesInput;
// use AppHttpControllersController;
class NewsController extends Controller
{
//Shownews method. This is where the individual news is shown with its comments.
    public function showNews($id)
    {
        $all = DB::table('news')->get();
        $news = News::find($id);
        return View::make('coin.shownews', compact('news','all'));
    }
}

注释控制器

<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppComment;
use AppNews;
use AppGraph;
use Validator;
use Session;

class CommentsController extends Controller
{
    public function store(Request $request, $news_id)
    {
        //
        $this->validate($request, array(
            'name'=> 'required | max:255',
            'email'=> 'required| email | max:255',
            'comment'=> 'required | min:5'
        ));
        $news = News::find($news_id);
        $comment = new Comment();
        $comment->name = $request->name;
        $comment->email = $request->email;
        $comment->comment = $request->comment;
        $comment->approved = true;
        $comment->news()->associate($news);
        $comment->save();

        // return $comment->toJson();
        return redirect()->route('article', [$news->id]);
}

它之所以表现得像这样,是因为您在调用视图时正在加载整个网页。因此,您需要将其分开并使用 ajax 来调用可以在视图中使用@include的数据。

Comments area
 <h4 class="comments-title" > <span class="fas fa-comment-alt"></span>
                {{$news->comments()->count()}}
                Comments</h4>
              <div class="row" >
                  <div class="col-md-12 col-md-offset-2" style="overflow-y: scroll; height: 400px;
                  width: 400px; " id="commentarea" >
                  @include('table_data')
                  </div>
              </div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script>
        $(document).ready(function() {
         setInterval(function() {
           var page = window.location.href;
           $.ajax({
           url: page+'/table,
           success:function(data)
           {
            $('#commentarea').html(data);
           }
           });
         }, 5000);
       });
           </script>

创建包含边栏选项卡,然后放置注释区所需的元素table_data.刀片.php

                      @foreach($news->comments as $comment)
                        <div class="comment" style="background-color: #f6efef;" >
                         <div class="author-info">
                          <img src={{"https://www.gravatar.com/avatar/" . md5(strtolower(trim($comment->email))) . "?s=50&d=retro" }} class="author-image" id="image">
                          <div class="author-name">
                               <h4>{{$comment->name}} </h4>
                        <p class="author-time"> {{  date('jS F, Y - g:iA' ,strtotime($comment->created_at)) }}</p>
                         </div>
                        </div>
                        <div class="comment-content">
                                {{$comment->comment}}
                        </div>
                        </div>
                      @endforeach

当然,您需要为它提供另一条路由,并在控制器中具有另一个功能

<?php
namespace AppHttpControllers;
use DB;
use IlluminateHttpRequest;
use AppNews;
use Validator;
use Image;
use View;
use Storage;
use IlluminateSupportFacadesInput;
// use AppHttpControllersController;
class NewsController extends Controller
{
//Shownews method. This is where the individual news is shown with its comments.

 public function showNews($id)
    {
        $new = News::find($id);
        return view('coin.shownews')->with('new', $new);
    }
 public function commentData($id);
    {
        $all = DB::table('news')->get();
        $news = News::find($id);
        return view('table_data', compact('news', 'all'))->render();
    }
}

然后将路线添加到网络.php

`Route::get('article/{id}/commentData', 'NewsController@commentData')`;
//you must change the name of the route base on your url just add the /table. Route can't be the same so you need to add that

希望对您有所帮助!

最新更新