title: how to control multiple ajax requests in laravel 5.2
body: <p>I am working on an application where different ajax requests fires depending upon different actions.</p>
  
  <p>For example, there is a chat window having send button. When i click on that button an empty message is sent with ajax, successfully. It work nice. But when I hit the send button too many times, at start some requests respond 200 (ok) but then it respond 500 (internal server error). Due to this the other requests that are going continuously like updateLastActivity also disturb.
  The preview of the error in developer's tool is:
  Whoops like something went wrong.</p>
  
  <p>Note: When I make this chat system in core PHP, it work fine. There is no internal server error when I send too may requests.</p>
  
  <p>Here is the code I am using</p>
  
  <pre><code>    //the following code is used to send the message
   $(document).on('click','.send_message_bt',function(event){
       event.preventDefault();
  
          var id=$(this).data('id');
          var name=$(this).data('name');
          var message=$("#message_field-"+id).val();
  
  
  
      $.ajax({
          //headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
           headers: { 'X-CSRF-TOKEN': {!! json_encode(csrf_token()) !!} },
          url:'{{route('user.sendmessage')}}',
          type:'POST',
          data:{
            id:id,
          message:message
             },
          success:function(data,status){
          //clear the message field value
          $("#message_field-"+id).val('');
  
          //update the chat history
          fetchChatHistory(id,name);
  
  
      },
          error:function(response){
             if(response.status==401){
                    alert('You are not logged in!');
                    window.location=window.location.href;
                     }
  
             }
  
  });
  
  });
  </code></pre>
  
  <p>here is the back end code</p>
  
  <pre><code>public function sendMessage(Request $request){
      $message=new Userchatmessage();
       $message-&gt;message=$request-&gt;message;
       $message-&gt;sender_id=Auth::user()-&gt;id;
       $message-&gt;receiver_id=$request-&gt;id;
       $message-&gt;save();
       return response('success');
  }
  </code></pre>
  
  <p>How to fix this issue.</p>