2017-07-28 10 views
1

誰かがlaravelのajaxで1つのフォームのすべてのデータを渡す方法を教えていただけますか?ajax投稿要求laravelでトークンを渡す方法は?

私は私の例を入れます、私はそれがトークンミスの原因を渡すことはできません。

Javascriptコード:

$(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); 
      } 
     }); 
    }); 
}); 

ブレードコード:

@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 TO LIST ALL THE 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 as $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 TO LIST ALL THE PROJECTS END HERE --> 

<div id="form1" style="display:none;" class="col-md-8"> <!-- DIV TO SHOW THE 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 TO SHOW THE 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 

すべてのヘルプは多くのことを感謝されます!私はstackoverflowで他の質問をチェックしますが、それを修正することはできません、私のコードで誰かができるか見てみましょう。 詳細については、お問い合わせください。 url関数が機能します!

また私はミドルウェアの例外に置くが、私はそれは良いアイデアではないと思う場合にのみ動作します

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

を試してみてください。

+0

urページを調べて、そこにcsrfトークンがありますか? _tokenの名前で入力していますか? – Sletheren

+0

それは@Sletherenです –

答えて

1

- 潜在的な苦境N°1:あなたの最初の形式では

{{ csrf_field() }} 

を削除し、<form>

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

直後にこれを入れて - 潜在的な修正N° 2:

config/session.phpドメインの値がnullであることを確認してください。

storage/framework/sessions/からキャッシュを削除し、storage/framework/views/

- 潜在的な修正N°3:{!! csrf_token() !!}の代わり

使用{{ csrf_token() }}

- 潜在的な苦境N°4:

linuxまたはmacの場合、セッションディレクトリにパーミッションがあることを確認してください:sudo chmod -R 777 Storageがその仕事をします。

- 潜在的な修正N°5:

は頭の中であなたのマスターレイアウトに追加します。

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

と、あなたはドンそのようにCSRFトークンを使用するすべてのあなたのAJAX要求を設定」提出するフォームに毎回添付する必要があります マスターレイアウトの最初のタグとして追加できます。

$.ajaxSetup({ 
    headers: { 
     'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') 
    } 
}); 

- 潜在的な修正N°6:すべてが失敗した場合

は、その後、あなたのVerifyCsrfToken.phpミドルウェアファイルに以下の行を追加することによって、アクセス制御を可能にします。

$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'); 
+0

それは仲間@ Sletherenは動作しません –

+0

答えを今すぐ確認してください – Sletheren

+0

ありがとうたくさん!今私は町から出て、私が来たら、私はそれを試してみよう!とにかく、nº6を修正する以外はすべて完了したと思います。どれどれ! –

関連する問題