First of all Hello , i have a kinda of problem that i cannot manage . am posting an ajax request like the following
<script>
$(document).ready(function(){
$(document).on('keydown' , '.message_input', function(e){
var token = $('input[name=_token]').val();
var msg = $(this).val();
var element = $(this) ;
var ul = element.parent().parent().find('ul');
var authenticated_user_id = $(this).attr('data-auth-id');
var user_id = $(this).attr('data-to-user-id');
var conversation_id = $(this).attr('data-conversation-id');
if(!msg == '' && e.keyCode == 13 && !e.shiftKey){
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
url : "{{ url("$lang/post_message_test")}}" ,
method : 'POST',
cache: false,
dataType:'JSON',
data: { msg:msg , authenticated_user_id:authenticated_user_id , user_id:user_id , conversation_id: conversation_id , _token:token },
success:function(data){
// my logic
}
});
return false ;
}
});
});
</script>
this will take with a chat controller and specific function called postMessage
it has a structure like the following
public function postMessage(Request $request ){
if($request->ajax()){
/** data coming from our post request with an elegant way */
$authenticated_user_id = $request->get('authenticated_user_id');
$user_id = $request->get('user_id');
$conversation_id = $request->get('conversation_id');
$msg = $request->get('msg');
// find user image
$user = User::find($authenticated_user_id) ;
$image = asset('uploads/users/').'/'.$user->profileImage ;
$name = '' ;
if($user->userType == 'doctor'){
$name = $user->doctorName ;
}else{
$name = $user->headPersonName ;
}
$message = new Message ;
$message->sender_id = $authenticated_user_id ;
$message->receiver_id = $user_id ;
$message->conversation_id = $conversation_id ;
$message->message = $msg ;
$message->is_read = 'not_read' ;
if($message->save()){
$msg=Message::where('id',$message->id)
->select('id','message','sender_id','receiver_id','created_at')
->with('sender')
->with('receiver')
->first();
$sender_name = $msg->sender->name ;
$sender_image = $msg->sender->profileImage ;
$dt = Carbon::now();
$message_date = $dt->diffForHumans() ;
event(new MessageWasRecieved( $user_id , $message->message , $image , $authenticated_user_id , $name ));
return Response::json([
'status'=>'success' ,
'message'=>$msg->message ,
'user_id'=>$user_id ,
'auth_id'=>Auth::user()->id ,
'image'=>$sender_image ,
'name'=>$sender_name
]);
}else{
return Response::json([
'status'=>'error' ,
]);
}
}
}
in that simple logic when it reaches to event function , it should trigger that event and in javascript am waiting to listen to this event .
what really happens is that after posting to this url post_message_test that what happens in network tab instead of returning a status code of 200 it gives me a 302 status ( check image below )
and call another url called campaign ( this is a url found also in my project ) . i have tried all possible cautions , i terrible checked my code and am sure of every thing is working perfect
so what is causing that and i hope i could described my problem correct
thanks