Laravel POST Token missmatch exception(文件上传)

时间:2015-05-15 10:29:16

标签: php jquery html ajax laravel

我有一个功能,创建一个弹出窗口,对它有一点响应(这个工作),我有这个HTML代码,这是我的弹出框(它使用jquery对话框显示弹出窗口,但是表单就在我的其他html代码之间,由一个使其不可见的div包围着:

<form id="logform" method="POST"> 
<select id='logoption'>  
<option value="0"> Select logtype </option>
<?php 
//SQL in laravel style to get log types from database
$logtypes = DB::table('time_log_types')->get();

foreach ($logtypes as $logtype)
{
echo ' <option value="'. $logtype->id . '">' . $logtype->logtype.'</option>';
}
?>
</select>
<div id="illness" style="display:none">
<p>End date: <input type="text" id="enddate"></p>
<p>Doctor's note: <input type="file" id="doctorsnote"></p>                                            
</div>                                           
<input type="button" value="submit" id="submitbutton" style="display:none" />
</form>

此弹出框上的提交按钮执行此操作(使用jquery):

<script>
$(document).ready(function() {
    //File upload
$('#submitbutton').on('click', function() {
            var fd = new FormData(document.getElementById("logform"));
//fd.append("CustomField", "This is some extra data");
            $.ajax({
                url: ' ',
                type: 'POST',
                data: fd,                
                cache: false,
                contentType: false,
                processData: false
            });
        });
    });

</script>

我确保我的POST实际上是通过在我的routes.php中添加此路由来到达正确的位置(我知道我发布到正确的位置,因为我在使用Inspect元素时检查了网络选项卡火狐):

Route::post('calendar', 'LogController@upload');

最后这是我在LogController中的代码:

<?php namespace App\Http\Controllers;

use Input;

class LogController extends Controller {

    public function upload() {       
        $file = Input::file('doctorsnote');
        $destinationPath = 'C://xampp/htdocs/proofofconcept/savedImages';
        // If the uploads fail due to file system, you can try doing public_path().'/uploads' 
        $filename = str_random(12);
        //$filename = $file->getClientOriginalName();
        //$extension =$file->getClientOriginalExtension(); 
        $upload_success = $file->move($destinationPath, $filename);

        if( $upload_success ) {
           return Response::json('success', 200);
        } else {
           return Response::json('error', 400);
        }
    }
}

问题是,当我点击提交按钮时,我收到了500响应并记录了此错误消息,但我不知道如何处理它:

> [2015-05-15 10:08:14] local.ERROR: exception
> 'Illuminate\Session\TokenMismatchException' in
> C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php:46
> Stack trace:
> #0 C:\xampp\htdocs\proofofconcept\laravel\app\Http\Middleware\VerifyCsrfToken.php(17):
> Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request),
> Object(Closure))
> #1 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(125):
> App\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request),
> Object(Closure))
> #2 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\View\Middleware\ShareErrorsFromSession.php(55):
> Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
> #3 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(125):
> Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request),
> Object(Closure))
> #4 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Session\Middleware\StartSession.php(61):
> Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
> #5 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(125):
> Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request),
> Object(Closure))
> #6 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse.php(36):
> Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
> #7 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(125):
> Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request),
> Object(Closure))
> #8 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\EncryptCookies.php(40):
> Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
> #9 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(125):
> Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request),
> Object(Closure))
> #10 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php(42):
> Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
> #11 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(125):
> Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request),
> Object(Closure))
> #12 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
> #13 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(101):
> call_user_func(Object(Closure), Object(Illuminate\Http\Request))
> #14 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(115):
> Illuminate\Pipeline\Pipeline->then(Object(Closure))
> #15 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(84):
> Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
> #16 C:\xampp\htdocs\proofofconcept\laravel\public\index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
> #17 {main}

我完全不知道发生了什么,我根据各地的指南创建了这段代码,据我所知,这应该是有效的,但我对所有这些都是新手,我真的可以使用一些帮助解决这个问题

编辑:我在表单中添加了一个新字段,以包含csrf标记,如下所示:

<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">

现在我得到了一个不同的堆栈跟踪:

[2015-05-15 11:21:23] local.ERROR: exception 'ReflectionException' with message 'Class App\Http\Controllers\LogController does not exist' in C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Container\Container.php:776
Stack trace:
#0 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Container\Container.php(776): ReflectionClass->__construct('App\Http\Contro...')
#1 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Container\Container.php(656): Illuminate\Container\Container->build('App\Http\Contro...', Array)
#2 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(644): Illuminate\Container\Container->make('App\Http\Contro...', Array)
#3 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(83): Illuminate\Foundation\Application->make('App\Http\Contro...')
#4 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(54): Illuminate\Routing\ControllerDispatcher->makeController('App\Http\Contro...')
#5 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Routing\Route.php(204): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'App\Http\Contro...', 'upload')
#6 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Routing\Route.php(134): Illuminate\Routing\Route->runWithCustomDispatcher(Object(Illuminate\Http\Request))
#7 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Routing\Router.php(701): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
#8 [internal function]: Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#9 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(141): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#10 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#11 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(101): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#12 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Routing\Router.php(703): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#13 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Routing\Router.php(670): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#14 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Routing\Router.php(628): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#15 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(214): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#16 [internal function]: Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#17 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(141): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#18 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php(43): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#19 C:\xampp\htdocs\proofofconcept\laravel\app\Http\Middleware\VerifyCsrfToken.php(17): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#20 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(125): App\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#21 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\View\Middleware\ShareErrorsFromSession.php(55): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#22 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(125): Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#23 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Session\Middleware\StartSession.php(61): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#24 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(125): Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#25 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse.php(36): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#26 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(125): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#27 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\EncryptCookies.php(40): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#28 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(125): Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#29 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php(42): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#30 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(125): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#31 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#32 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(101): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#33 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(115): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#34 C:\xampp\htdocs\proofofconcept\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(84): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#35 C:\xampp\htdocs\proofofconcept\laravel\public\index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#36 {main}  

编辑:我将我的端点移动到另一个控制器并且它现在正常工作,但现在却抱怨我的Input :: file(&#39; doctorsnote&#39;)是非对象:

[2015-05-15 11:35:20] local.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Call to a member function move() on a non-object' in C:\xampp\htdocs\proofofconcept\laravel\app\Http\Controllers\CalendarController.php:27
Stack trace:
#0 {main}  
编辑:我现在已经为我的表单提供了这个,但是当我点击提交按钮时,它没有注册,POST没有被执行

<form id="logform" enctype="multipart/form-data" action="calendar"  method="POST"> 
<select id='logoption'>  
<option value="0"> Select logtype </option>
<?php 
//SQL in laravel style to get log types from database
$logtypes = DB::table('time_log_types')->get();    
foreach ($logtypes as $logtype)
{
echo ' <option value="'. $logtype->id . '">' . $logtype->logtype.'</option>';
}
?>
 </select>

<div id="illness" style="display:none">
<p>End date: <input type="text" id="enddate"></p>
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<p>Doctor's note: <input type="file" name="doctorsnote" id="doctorsnote"/></p>                                            
                                            </div>                                            

<input type="button" value="submit" id="submitbutton" style="display:none" />
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
</form>

2 个答案:

答案 0 :(得分:0)

清除浏览器的缓存,包括cookie。 Ctrl+Refresh表单页面。再次尝试提交。

修改 只需在表单中包含此元素即可。

<input type="hidden" name="_token" value="{{ csrf_token() }}">

答案 1 :(得分:0)

我刚设法上传了一个文件:D 我怎么做了:

第1步:编辑我的表单像这样: - 添加了enctype,在我的表单中添加了一个动作,添加了方法帖子 - 为csrftoken添加了隐藏字段 - 删除了与提交表单相关的所有javascript(不需要这个ajax调用)

<form id="logform" enctype="multipart/form-data" action="calendar"  method="post"> 
<select id='logoption'>  
<option value="0"> Select logtype </option>
<?php 
//SQL in laravel style to get log types from database
$logtypes = DB::table('time_log_types')->get();

foreach ($logtypes as $logtype)
{
echo ' <option value="'. $logtype->id . '">' . $logtype->logtype.'</option>';
}
?>
</select>

<div id="illness" style="display:none">
<p>End date: <input type="text" name="enddate" id="enddate"></p>
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<p>Doctor's note: <input type="file" name="doctorsnote" id="doctorsnote"/></p>                                            
</div>    
<input type="button" value="submit" id="submitbutton" style="display:none" onclick="submit()" />
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
</form>

第2步:删除javascript

步骤3:将上传功能移至我的calendarController并将路线更改为:

Route::post('calendar', 'CalendarController@upload');

我没有更改任何其他内容(除了删除整个javascript函数)

它的功能就像一个魅力,花了我一整天才能让它工作但我觉得对于一个完整的新手来说并不坏:D