即時通過Laravel 4構建應用程序,在某些時候我想通過模式(Bootstrap)添加一些模型,所以我需要ajax來發送我的消息,我在控制器中設置了我的路線和動作,然後我用刀片建立了表格標記,我寫了ajax代碼,請求很好,我通過Input facade檢索輸入,這裏的問題是表單有一個文件輸入,當使用$('#formRub').serialize()序列化表單數據時,它無法處理文件輸入,所以我必須使用FromData對象,並在ajax請求中將processData和contentType設置爲false,請求發送,但我當你訪問輸入門面我有空陣列!Laravel Ajax Input :: all()通過FormData發送時返回空值
路線:
Route::post('/add', ['as' => 'rubrique.add.post', 'uses' => '[email protected]']);
控制器:
class RubriquesController extends \BaseController {
public function ajaxaddpost(){
return dd(Input::all());
$v = Validator::make(Input::all(), Rubrique::$rules);
if($v->fails()){
return Response::json([
'fail' => true,
'errors' => $v->errors()->toArray()
]);
}
if(Input::hasFile('image'))
return Response::json(['success' => Input::file('image')]);
return Response::json(['fail' => 400]);
}
標記:
{{ Form::open(['route' => 'rubrique.add.post', 'method' => 'post', 'files' => true, 'class' => 'form-horizontal', 'id' => 'rubForm']) }}
{{Form::label('name', 'Nom de la boutique :', ['class' => 'col-md-4 control-label'])}}
{{Form::text('name', null, ['class' => 'form-control', 'placeholder' => 'Entrer votre nom de boutique..'])}}
{{Form::label('desc', 'Description :', ['class' => 'col-md-4 control-label'])}}
{{Form::textarea('desc', null, ['class' => 'form-control', 'placeholder' => 'Enter votre e-mail..', 'rows' => '3'])}}
{{Form::label('image', 'Image :', ['class' => 'col-md-4 control-label'])}}
{{Form::file('image', ['class' => 'form-control', 'placeholder' => 'Enter votre e-mail..'])}}
{{Form::label('rubrique_id', 'Rubrique Parent :', ['class' => 'col-md-4 control-label'])}}
{{ Form::rubriques(0) }}
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
{{Form::submit('Ajouter', ['class' => 'btn btn-primary', 'id' => 'sendRubrique']) }}
</div>
</div>
{{Form::close()}}
JS:
$('#rubForm').submit(function(e){
e.preventDefault();
var $form = $(this),
dataFrom = new FormData($form),
url = $form.attr("action"),
method = $form.attr("method");
$.ajax({
url: url,
data: dataFrom,
type: method,
contentType: false,
processData: false
});
});
試試這個插件 http://blueimp.github.io/jQuery-File-Upload/ – justrohu 2014-10-02 10:06:51
我不想上載一個插件,我只需要在只有這種形式上傳,這樣可以幫助我爲什麼這不起作用? – 2014-10-02 10:20:14