【发布时间】:2014-11-27 07:21:30
【问题描述】:
我正在使用 Laravel 4 构建一个应用程序,在某些时候我想通过 modal(Bootstrap) 添加一些模型,所以我需要 ajax 来发送我的来源,我已经在控制器中设置了我的路由和操作,然后我已经构建了使用刀片进行表单标记,我已经编写了 ajax 代码,请求正常,我通过 Input 门面检索输入,这里的问题是表单有一个文件输入,并且在使用 $('#formRub') 序列化表单数据时。 serialize(),它不能处理文件输入,所以我必须使用 FromData 对象,并在 ajax 请求中将 processData 和 contentType 设置为 false,请求发送,但是当你访问 Input 门面时,我得到了空数组! !
路线:
Route::post('/add', ['as' => 'rubrique.add.post', 'uses' => 'RubriquesController@ajaxaddpost']);
控制器:
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
});
});
【问题讨论】:
-
我不需要用于上传的插件,我只需要以这种形式上传,所以你能帮我解释一下为什么这不起作用吗?
标签: javascript php jquery ajax laravel-4