在使用LARAVEL和PUSHER频道创建WEB通知时,在控制台中收到错误消息'Uncaught ReferenceError:未定义Pusher'

时间:2019-04-05 11:47:58

标签: php laravel pusher

我正在尝试使用https://pusher.com向网络发送通知 我已经实现了推入器文档中给出的所有内容,但现在仍然在控制台中收到类似“ Uncaught ReferenceError:未定义推入器”的错误

请检查我的代码中是否存在任何问题 我已经在App \ Events中创建了FormSubmitted事件 看起来像

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class FormSubmitted implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
    public $text;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($text)
    {
        //
        $this->text = $text;
    }

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

    public function broadcastAs()
    {
        return 'form-submitted';
    }
}

================================================ ========================

我已经在资源文件夹中创建了counter.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Pusher Test</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script
  <script src="https://js.pusher.com/4.4/pusher.min.js"></script>
  <script>

    // Enable pusher logging - don't include this in production
    Pusher.logToConsole = true;

    var pusher = new Pusher('MY_KEY_PLACED_HERE', {
      cluster: 'ap2',
      forceTLS: true
    });

    var channel = pusher.subscribe('my-channel');
    channel.bind('form-submitted', function(data) {
      alert(JSON.stringify(data));
    });
  </script>
</head>
<body>
  <h1>Pusher Test</h1>
  <p>
    Try publishing an event to channel <code>my-channel</code>
    with event name <code>my-event</code>.
  </p>
</body>
</html>

================================================ ==================

然后我在资源内部创建sender.blade.php

<form action="sender" method="post">
    {{csrf_field()}}
    <input type="text" name="text">
    <input type="submit" value="submit">
</form>

================================================ ======== 我已经在.env文件中设置了所有凭据(在这里我已经设置了正确的密钥和机密)

BROADCAST_DRIVER=pusher
PUSHER_APP_ID=XXXX
PUSHER_APP_KEY=XXXXX
PUSHER_APP_SECRET=XXXX
PUSHER_APP_CLUSTER=AP2

================================================ ========

and finally `web.php` looks like 


Route::get('/counter', function () {
    return view('counter');
});

Route::get('/sender', function () {
    return view('sender');
});

Route::post('/sender', function () {

    $text = request()->text;

    event(new FormSubmitted($text));
});

================================================ ============ 当我在浏览器中运行localhost / counter

我在控制台中收到“未捕获的ReferenceError:未定义Pusher”这个错误

我希望当我从文本发送localhost / sender的一些输入时,输出该文本数据作为通知

1 个答案:

答案 0 :(得分:0)

您的header script标记中有错字。您没有正确结束jQuery </script标签。将其更改为</script>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>