0
我使用laravel php和XMLHttpRequest的(上載圖像)和我得到以下錯誤Laravel的XMLHttpRequest MethodNotAllowedHttpException在RouteCollection.php線218:
MethodNotAllowedHttpException在RouteCollection.php線218
以下是我處理表單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...');
});
}
這是我的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>
這是我的路線文件:
路線::組([ '前綴'=> '管理員'],功能( ){
Route::get('/', function() {
return view('welcome');
});
Route::auth();
Route::get('/home', [
'uses'=>'[email protected]',
'as'=>'home'
]);
Route::post('/productAdd', [
'uses'=>'[email protected]',
'as'=>'product.add'
]);
Route::post('/productImageAdd', [
'uses'=>'[email protected]',
'as'=>'productImage.add'
]);
});
最後,這是我的帖子控制器代碼:
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);
}
}
CSRF保護怎麼樣?它到位了嗎? –