【发布时间】:2017-03-11 08:49:32
【问题描述】:
我在项目中集成 dropzone.js 时遇到问题。我使用 ajax 函数上传图像并返回值。但我收到 302 错误和 csrf 错误。为什么会这样?如何解决这个问题?
我的代码查看页面
<form method="POST" action="{{lurl('post-events')}}" enctype='multipart/form-data'>
<label>event title*</label>
<input type="text" class="form-control" name="name" placeholder="give it a short distinct name">
<div class="image_drop">
<!--<img src="images/upload-files-here.png" pagespeed_url_hash="19921898" onload="pagespeed.CriticalImages.checkImageForCriticality(this);"/>
-->
<div class="dropzone" id="mydropzone" name="mydropzone">
</div>
<p>we recommend usung at least a 2160x1080px(2:1ratio) image thats no
larger than 10MB learn more.</p>
</div>
</form>
我用jquery调用ajax。
$("#mydropzone").dropzone({ url: "event-image" });
Route.php
Route::post('event-image','HomeController@getImage1');
控制器功能
public function getImage1() {
$input = Input::all();
$rules = array(
'file' => 'image|max:3000',
);
$validation = Validator::make($input, $rules);
if ($validation->fails()) {
return Response::make($validation->errors->first(), 400);
}
$file = Input::file('file');
$extension = File::extension($file['name']);
$directory = public_path().'/uploads/pictures/events';
$filename = sha1(time().time()).".{$extension}";
$upload_success = Input::upload('file', $directory, $filename);
if( $upload_success ) {
return Response::json('success', 200);
} else {
return Response::json('error', 400);
}
}
在我的控制台中我收到此错误
POST http://localhost/Classified/en/event-image 302 Found
GET http://localhost/Classified/en/events?error=CsrfToken
【问题讨论】:
-
302 表示永久重定向,表示您的路线被重定向到其他页面
标签: php jquery ajax laravel-5.2 dropzone.js