jquery - 为什么数据不显示在laravel的视图文件中?
问题描述
我有一个刀片文件,代码如下。
<div id="id1">0</div>
<div id="id2">0</div>
<div id="id3">0</div>
每个0
div 中的 应该由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。
我怎么能这样做?有人可以帮忙吗?谢谢
解决方案
推荐阅读
- reactjs - 如何在 React 中创建一个可以在不同组件中访问的全局变量?
- r - 将行名转换为值到列并保留相应的值
- proxy - 如何在 JAMstack 解决方案中代理 API 请求?
- firebase - NoSQL 数据库结构
- c# - 这个 Levenshtein 距离算法是否正确?
- node.js - Cloud Functions 和 Express req.headers.cookie 未定义
- python-3.x - 数组 - 随机机会
- c# - 多个 UDP 连接到具有相同端口的不同远程 IP
- javascript - Chart.js 多色填充部分
- javascript - 引用画布时的空指针