Dobry day, do a chat on laravel 5.6, receive from the controller is implemented, sending messages does not work,

here is the controller code to send messages

namespace App\Http\Controllers\AjaxChat; use Illuminate\Http\Request; use App\Http\Controllers\Controller; class SendChatController extends Controller { public function index(Request $request) { // отправка сообщения $sender = $request->sender(); $text = $request->message(); $message = "\n<li id='down' class='left clearfix'>$sender написал: $text</li>"; // шаблон нашего сообщения $filename = asset('storage/txts/messages.txt'); $file = fopen($filename, 'a'); // открываем файл для редакции fwrite($file, $message); // записываем отправленное сообщение в чат fclose($file); // закрываем фай } } 

here's the chat.blade.php code

 <head> <meta name="_token" content="{{ csrf_token() }}"> </head> <!-- jQuery library --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <!-- Latest compiled JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"> </script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <link rel="stylesheet" href="{{asset('css/chat.css')}}"> <script> //ajax setup $(function() { $.ajaxSetup({ headers: { 'X-CSRF-Token': $('meta[name="_token"]').attr('content') } }); }); //send func function send() { var name = $('.name>input').val(); var message = $('.message>input').val(); // сообщение $.post( "/Chat/Send", {'sender': name, 'message': message} ); } //show messages function show() { $.ajax({ url: '{{route("chatShow")}}', timeout: 10000, // время ожидания success: function(data) { $('#messages').html(data); }, error: function() { $('#messages').html("Не удалось загрузить"); } }); } var interval = 5000; // ожидание show(); //auto messages update setInterval('show()', interval); </script> <div class="container"> <div class="row"> <div class="col-lg-12"> <div class="panel panel-primary" style="max-height: 300px;"> <div class="panel-heading"> <span class="glyphicon glyphicon-comment"></span> Chat <div class="btn-group pull-right"> </div> </div> <div class="panel-body scroll" style=" max-height: 200px; overflow: auto;"> <ul id="messages" style="list-style-type: none;"> </ul> <div class="down"></div> </div> <div class="panel-footer"> <form method="POST"> <div class="name"> <input id="btn-input" type="text" class="form-control input- sm" placeholder="Type your name here..." /> </div> <div class="input-group"> <div class="message"> <input id="message" type="text" class="form-control input-sm" placeholder="Type your message here..." /> </div> <span class="input-group-btn"><button onclick="send()" type="submit" id="send_form" class="btn btn-warning btn-sm" id="btn-chat"> Send</button> </span> </div> </form> </div> </div> </div> </div> </div> <div class="alert alert-primary" id="send_message_result"> </div> </body> </html> 

Standard ajax chat, without laravel works, but not with it, here is the route

 //Send Route::post('/Chat/Send','AjaxChat\SendChatController@index')->name('chatSend'); 
  • Error gives or what? It's hard to understand why you are not working. And how it should work. - Ilya Zelenko
  • no, it simply writes' Error 500 / Chat / Send (iternal server error) 'in the browser console and immediately sees the laravel error screen. MethodNotAllowedHttpException' - Gweston
  • I found that you just need to first put the type of request in ajax js first post then reload the page, then get reload, and yes you guessed again post and reload and it worked miracle (I spent three days with it, but I hope that this will help someone ) - Gweston

2 answers 2

Try to disable verification in VerifyCsrfToken verification $ except = ['/ Chat / Send',];

    I found that first you just need to put the type of request in ajax jpeg first post then reload the page, then get reload, and yes you guessed again post and reload and it worked miracle (I spent three days with etim, but I hope that this will help someone ) ps (for beginners) do not need to write your sentences as the author himself found a solution