php - Posting file by ajax "error 405 Method not Allowed" in Laravel -
route
route::get('/mediafile', 'mediacontroller@index');
mediacontroller
public function store(request $request) { $image = $request->file('userfile'); if($request->hasfile('userfile')) { $filename = $image->getclientoriginalname(); image::make($image)->resize(300, 300)->save(public_path('media/' . $filename)); $media = new media(); $media->description = $request->description ? $request->description : ''; $media->mediapath = $filename; $media->medianame = $filename; $media->save(); } return response()->json($media); }
view
<div class="form-group image"> <div class="col-xs-12 col-sm-12 col-md-12"> <label>gambar</label><input type="file" name="userfile" id="userfile" style="display: inline"> <label style="margin-left: 25px">atau</label> <a href="http://192.168.1.12/melody4/media/select.html" class="pilihmedia btn btn-default">pilih dari media</a> <input type="hidden" id="mediaid" name="categorymediaid" value="" /> <br /><br /> <span class="uploadstatus"></span> <div class="success infomedia infomedia alert alert-success" style="display: none"> </div> </div>
ajax
$('#userfile').change(function(){ $(this).attr('disable',true); $('.uploadstatus').html('sedang mengupload file <img src="{{ asset('vendor/img/loading.gif') }}" alt="ajaxloading" />'); $(this).parent().ajaxsubmit({ url: "<?php echo url('/mediafile') ?>", datatype: 'json', type: "post", success : function(data){ $(this).attr('disable',false); $('.uploadstatus').empty(); }, error : function(data){ } }); });
i got error "405 method not allowed" using ajax when posted image, have researched , followed answers in stack overflow, still can't resolve it.
please me solve it.
may issue in csrf token add line in html
<meta name="_token" content="{{ csrf_token() }}"/>
and in ajax
$.ajaxsetup({ headers: { 'x-csrf-token' : $('meta[name=_token]').attr('content') } });
and need post image in ajax call
Comments
Post a Comment