[英]Laravel XMLHttpRequest MethodNotAllowedHttpException in RouteCollection.php line 218:
I'm uploading image using laravel php and XMLHttpRequest() and i am getting following error 我正在使用laravel php和XMLHttpRequest()上传图像,但出现以下错误
MethodNotAllowedHttpException in RouteCollection.php line 218 RouteCollection.php第218行中的MethodNotAllowedHttpException
following is my javascript code that handles the form submit: 以下是我处理表单提交的javascript代码:
$(document).on('submit','form',function(e){
e.preventDefault();
$form=$(this);
uploadImage($form);
});
function uploadImage($form){
$form.find(".progress-bar").removeClass("progress-bar-success").removeClass("progress-bar-danger");
var formdata=new FormData($form[0]);
var request=new XMLHttpRequest();
// progress event...
request.upload.addEventListener('progress',function(e){
var percent=Math.round(e.loaded/e.total * 100);
console.log('progress: '+percent);
$form.find('.progress-bar').width(percent+'%').html(percent+'%');
});
// progress completed load event
request.upload.addEventListener('load',function(e){
var percent=e.loaded/e.total * 100;
console.log('load: '+percent);
$form.find('.progress-bar').addClass('progress-bar-success').html('upload completed');
});
request.open('post','productImage.add');
request.send(formdata);
$form.on('click','.cancel',function(){
request.abort();
$form.find('.progress-bar').addClass('progress-bar-danger').removeClass('progress-bar-success').html('upload aborted...');
});
}
This is my html form whose info is sent to the php route 这是我的html表单,其信息发送到php路由
<form role="form">
<label>Product Image:</label>
<input type="file" name="image">
<button type="submit" class="btn btn-info btn-sm upload">Upload</button>
<button type="button" class="btn btn-danger btn-sm cancel">Cancel</button>
<div class="progress progress-striped active">
<div class="progress-bar" style="width:0%"></div>
</div>
</form>
this is my routes file: 这是我的路线文件:
Route::group(['prefix' => 'admin'], function () {
Route::get('/', function () {
return view('welcome');
});
Route::auth();
Route::get('/home', [
'uses'=>'HomeController@index',
'as'=>'home'
]);
Route::post('/productAdd', [
'uses'=>'productsController@add',
'as'=>'product.add'
]);
Route::post('/productImageAdd', [
'uses'=>'productsController@addImage',
'as'=>'productImage.add'
]);
});
and finally this is my post controller code: 最后这是我的后控制器代码:
class productsController extends Controller
{
function add(Request $request){
$product= new product();
$product->productName=$request["name"];
$product->description=$request["description"];
$product->discount=$request["discount"];
$request->user()->products()->save($product);
return redirect()->route('home');
}
function addImage(Request $request){
print_r($request);
}
}
You forgot the token for CSRF protection. 您忘记了CSRF保护的令牌。 Add this hidden field to your form:
将此隐藏字段添加到您的表单:
<input type="hidden" name="_token" value="{{ csrf_token() }}">
That should do the trick! 这应该够了吧!
Edit: dont forget to send the _token with the request your making. 编辑:不要忘记发送_token和您的请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.