javascript - How to pass token in ajax post request laravel? -


someone can show me how pass data of 1 form in ajax on laravel?

i put example, can't pass cause token miss.

javascript code:

$(document).ready(function(){      $("#buttoncreate").click(function(){         $("#listall").hide();         $("#form1").fadein(1000);      });      $("#createprojectsubmit").click(function(){         $("#myform").submit();     });      $("#myform").submit(function(e){         e.preventdefault();         $.ajax({             url:'/admin/projects/postupload',             type:'post',             data:$('#myform').serializearray(),             success: function(){                 $("#form1").fadeout(1000);                 $("#form2").fadein(1000);             }         });     }); }); 

blade code:

@extends('cms.public.layouts.default') @section('content') <meta name="csrf-token" content="{{ csrf_token() }}">  <div class="col-md-10">     <h3 style="letter-spacing:40px;text-align:center;color:f15d5e;">proyectos</h3> </div>  <div id="listall"> <!-- div list projects start here -->         <div class="col-md-2" style="padding:20px;">           <button type="button" id="buttoncreate" class="btn btn-danger">crear proyecto</button>          </div>                       <table class="table">                   <thead style="color:white">                     <tr>                       <th>id</th>                       <th>slug</th>                       <th>order</th>                       <th>public</th>                       <th>path header</th>                       <th>path home</th>                       <th>fecha creación</th>                       <th>fecha ultima actualización</th>                       <th><span class="glyphicon glyphicon-cog"></span></th>                     </tr>                   </thead>                   <tbody style="color:white">                   @foreach ($projects $key => $project)                     <tr>                       <th>{{$project->id}}</th>                       <td>{{$project->slug}}</td>                       <td>{{$project->order}}</td>                       <td>{{$project->public}}</td>                       <td>{{$project->pathheader}}</td>                       <td>{{$project->pathhome}}</td>                       <td>{{ date('m j, y', strtotime($project->created_at))}}</td>                       <td>{{ date('m j, y', strtotime($project->updated_at))}}</td>                       <td><a href="{{ route('admin.projects.show', $project->id)}}" class="btn btn-info btn-sm">view</a> <a href="{{ route('admin.project.edit', $project->id)}}" class="btn btn-success btn-sm">edit</a>                   @endforeach                     </tr>                   </tbody>                 </table>   <br><br> </div>  <!-- div list projects end here -->  <div id="form1" style="display:none;" class="col-md-8"> <!-- div show create project form 1 start here-->     <div>     <h3>crear nuevo proyecto</h3>     </div>     <div id="formcreateproject">         <form method="post" action="{{ route('admin.projects.store') }}" enctype="multipart/form-data" id="myform" name="myform">         {{ csrf_field() }}           <div class="form-group">             <label name="title">slug:</label>             <input type="text" id="slug" name="slug" placeholder="ejemplo-de-slug" class="form-control form-control-sm">             <label name="order">order:</label>             <input type="number" id="order" name="order" class="form-control form-control-sm">             <label name="public">public:</label>             <input type="number" id="public" name="public" class="form-control form-control-sm">              <label name="body">header</label>             <input type="file" name="pathheader" id="pathheader"  class="form-control-file" aria-describedby="filehelp"><br>             <label name="body">home</label>             <input type="file" name="pathhome" id="pathhome" class="form-control-file" aria-describedby="filehelp"><br>              <input type="submit" value="crear proyecto" id="createprojectsubmit" class="btn btn-danger btn-md">             <input type="hidden" name="_token" value="{{ session::token() }}">             <br><br><br>            </div>         </form>        </div> </div> <!-- div show create project form 1 end here-->  <div id="form2" style="display:none;" class="col-md-6"> <div class="col-md-">     <h3>crear nuevo proyecto</h3>     </div>       <form method="post" action="{{ route('admin.projects.store') }}" enctype="multipart/form-data">           <div class="form-group">             <label name="title">slug:</label>             <input type="text" id="slug" name="slug" placeholder="ejemplo-de-slug" class="form-control form-control-sm">             <label name="order">order:</label>             <input type="number" id="order" name="order" class="form-control form-control-sm">             <label name="public">public:</label>             <input type="number" id="public" name="public" class="form-control form-control-sm">              <label name="body">header</label>             <input type="file" name="pathheader" id="pathheader"  class="form-control-file" aria-describedby="filehelp"><br>             <label name="body">home</label>             <input type="file" name="pathhome" id="pathhome" class="form-control-file" aria-describedby="filehelp"><br>              <input type="submit" value="crear proyecto" id="createprojectsubmit" class="btn btn-danger btn-md">             <input type="hidden" name="_token" value="{{ session::token() }}">             <br><br><br>            </div>         </form> </div>  </div> @stop 

any appreciate lot! check others questions in stackoverflow can't fix it, let's see if code can. if need more information please, ask it. url function works!

also try

https://laravel.com/docs/5.4/csrf#csrf-x-csrf-token

it works if put in exception of middleware think it's not idea.

- potential fix n°1:

in first form, delete the

 {{ csrf_field() }} 

and put directly after <form>

<input type="hidden" name="_token" value="{{ session::token() }}"> 

- potential fix n°2:

make sure inside config/session.php value of domain null.

and delete cache storage/framework/sessions/ , storage/framework/views/

- potential fix n°3:

use {!! csrf_token() !!} instead of {{ csrf_token() }}

- potential fix n°4:

if on linux or mac, make sure session dir has permission: sudo chmod -r 777 storage job.

- potential fix n°5:

add master layout in head:

<meta name="csrf-token" content="{{ csrf_token() }}"> 

and configure ajax requests use csrf token, way don't need attach everytime in forms u're submitting can add first tag in master layout.

$.ajaxsetup({     headers: {         'x-csrf-token': $('meta[name="csrf-token"]').attr('content')     } }); 

- potential fix n°6:

if fails, allow access-control adding these lines verifycsrftoken.php middleware file.

$response->headers->set('access-control-allow-origin' , '*'); $response->headers->set('access-control-allow-methods', 'post, get, options, put, delete'); $response->headers->set('access-control-allow-headers', 'content-type, accept, authorization, x-requested-with, application'); 

Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -