marioeid
9 months ago

how to pass the csrf_token to my view using ajax jquery

Posted 9 months ago by marioeid

the ajax function works but when i append i don't know how to pass the csrf_token to the jquery append function

 $('#post_create').on('click', function(e){
    
    //  var url=$(this).attr('action');
    //  var post=$(this).attr('method');
    e.preventDefault();
    //  var body=$('#body').val();
    //  var tilte=$('#title').val();
    $.ajax({
        // headers: {
        //     'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        //   }
        //   ,        
    url:'/createpost',
    type:'POST',
    data:$(".postform").serialize(),
    dataType: 'JSON',
   success:function(data)
   {
     
      if(data.status == "success"){


      $('#posts').append('<li class="arrow_box col-md-10 col-lg-10 offset-lg-1"><p><span id="title">'+data.data.title+'</span><br/><span id="body">'+data.data.body+'</span>               <br/><div class="info"><p> <div id="funform"> <div>'+data.data.created_at+'</div><a  class="fa fa-trash post_delete" data-id="'+data.data.id+'" data-url="/deletepost/'+data.data.id+'"></a><a class="fa fa-thumbs-up post_like"></a>   <a class="fa fa-comment post_comment"></a> <a class="fa fa-edit post_edit" </a></a>  </div> </p>  </div></p></li>');
      
      $('.close').click();
      $( '.postform' ).each(function(){
        this.reset();
    });
    
    }
     }
   ,error:function(data)
   {
     $('#errors').show();
     $('#errors').html('');
       var errors=data.responseJSON;
       $.each(errors,function(v,k)
       
       {
        $('#errors').append(v+"<br/>");

       }
       );
   }
   
    })



    });

my view

<div class="container-fluid">
        <div class="page col-lg-12">
          
            <ul id="posts" class="row">
                 @foreach($posts as $post)
                    <li class="arrow_box col-md-10 col-lg-10 offset-lg-1">
                    <p><span class="username">{{$post->user->name}} </span> &nbsp<span id="title">{{$post->title}}</span>
                    
                    <br/>
               <span id="body">{{$post->body}}</span>
                                   <br/>
                    <div class="info">
                    <p> 
                   <div id="funform">
                    <div>{{$post->updated_at->format('d M Y - H:i:s')}}</div>
                  <a  class="fa fa-trash post_delete" data-token="{{ csrf_token() }}" data-id="{{$post->id}}" data-url="/deletepost/{{$post->id}}"></a>
                   <a class="fa fa-thumbs-up post_like"></a>
                   <a class="fa fa-comment post_comment"></a>
                   <a class="fa fa-edit post_edit" </a></a>
                   </div> 
                   </p>
                   </div>
                    
                    </p>
                    
                    </li>
                @endforeach
                
            </ul>
             <button class="btn mybtn right" data-toggle="modal" data-target="#exampleModalCenter">post</button>
 
            <!-- Button trigger modal -->

Please sign in or create an account to participate in this conversation.