2017-07-28 100 views
1

有人可以告訴我如何通過laravel上的ajax傳遞一個表單的所有數據?如何在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

你檢查了你的頁面,並發現了csrf令牌嗎?在_token名稱的輸入中? – Sletheren

+0

它是@Sletheren –

回答

1

- 潛在修復N°1:

在第一個形式中,刪除

{{ csrf_field() }} 

和後直接<form>

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

將這個 - 潛在修復N° 2:

請確保您的config/session.php中的值爲空。

和從storage/framework/sessions/刪除緩存和storage/framework/views/

- 潛在修復N°3:

使用代替{!! csrf_token() !!}{{ csrf_token() }}

- 潛在修復N°4:

如果在linux或mac上,請確保Session dir具有權限:a sudo chmod -R 777 Storage將完成這項工作。

- 可能的解決N°5:

添加到您的總綱發展藍圖的頭:

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

和配置所有的Ajax請求使用CSRF令牌,這樣,你不」 t需要每次都以我要提交的表格附上它 您可以添加爲主佈局中的第一個標籤。

$.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之外,它已經完成了。讓我們來看看! –