首页 > 解决方案 > 为什么数据不显示在laravel的视图文件中?

问题描述

我有一个刀片文件,代码如下。

<div id="id1">0</div>

<div id="id2">0</div>

 <div id="id3">0</div>

每个0div 中的 应该由time()控制器替换。

控制器

 public function get($divId = null)
    {

     return UpdatedTime::dispatch($this->getTime($divId));
    }
     public function getTime($divId )
    {

     // pusher
        $options = array(
            'cluster' => 'ap3',
            'useTLS' => true
        );

        $pusher = new Pusher(
            env('PUSHER_APP_KEY'),
            env('PUSHER_APP_SECRET'),
            env('PUSHER_APP_ID'),
            $options
        );

        $pusher->trigger('my-channel', 'UpdatedTime', ['target' => $divId, 'value' => time()]);


    }

我可以在pusher中触发事件,但不能在视图文件中触发。

js

$(document).ready(function(){
 $.ajaxSetup({
     headers: {
          'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

  document.querySelectorAll('*').forEach(function(node) {
    var data = '';
    ajax = $.ajax({
      url: '/getTime/' + node.id,
      method: 'get',
      dataType: 'json',
      success: function(data) {
         //parse the server side response
         console.log(data);
         ajax = null;
      }
    });

    //node.innerHTML = ajax.data;

  });
});
// Pusher
    var pusher = new Pusher('xxxxxxxxxxxxx', {
        cluster: 'ap3',
        encrypted: true
      });
   // Subscribe to the channel we specified in our Laravel Event
      var channel = pusher.subscribe('my-channel');
   // Binding a function to a Event
     channel.bind('App\Events\UpdatedTime', function(data){
     document.getElementById(data.UpdatedTime.target).innerHTML = data.UpdatedTime.value; //**It is not working**
    

事件文件

  public function __construct($timechanged)
    {
       $this->timechanged = $timechanged;

    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('my-channel');
    }

路线/web.php

Route::get('/getTime/{id}', 'App\Http\Controllers\TimeController@getTime');

为什么我无法获取视图文件中的数据,访问 URL 后http://www.project.test/getTime/2会出现空白页面。我需要特定 ID 的带有 time() 的 div。

我怎么能这样做?有人可以帮忙吗?谢谢

标签: jquerylaravel

解决方案


推荐阅读