即时通过Laravel 4构建应用程序,在某些方面我想通过模态(Bootstrap)添加一些模型,所以我需要ajax来发送我的,我已经在控制器中设置我的路由和操作,然后我已经构建了用刀片形成标记,我写了ajax代码,请求顺利,我通过输入外观检索输入,这里的问题是表单有文件输入,并且用$(' #formRub序列化表单数据')。serialize(),它不能处理文件输入,所以我必须使用FromData对象并在ajax请求中将processData和contentType设置为false,发送请求,但是当你访问时我输入外观我有空阵!
路线:
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
});
});
答案 0 :(得分:3)
密钥在你的ajax请求中。在控制器中,你可以做任何你想做的事。
var form = document.forms.namedItem("yourformname"); // high importance!, here you need change "yourformname" with the name of your form
var formdata = new FormData(form); // high importance!
$.ajax({
async: true,
type: "POST",
dataType: "json", // or html if you want...
contentType: false, // high importance!
url: '{{ action('yourController@postMethod') }}', // you need change it.
data: formdata, // high importance!
processData: false, // high importance!
success: function (data) {
//do thing with data....
},
timeout: 10000
});
答案 1 :(得分:1)
您的JavaScript应如下所示:
$('#rubForm').submit(function(e){
e.preventDefault();
var $form = $( this ),
dataFrom = $form.serialize(),
url = $form.attr( "action"),
method = $form.attr( "method" );
$.ajax({
url: url,
data: dataFrom,
type: method,
processData: false
});
});
您应该使用$form.serialize()
,并且必须删除contentType: false,
现在,如果您将控制器放入控制器中,例如:
file_put_contents("test.txt", var_export(Input::all(), true));
它将创建包含数据的文件但是我不知道它是否适用于文件输入
修改强>
我没有注意到问题中的seralize()和文件输入,所以现在,您应该在表单中添加name属性:
{{ Form::open(['route' => 'rubrique.add.post', 'method' => 'post', 'files' => true, 'class' => 'form-horizontal', 'id' => 'rubForm', 'name' =>'myform']) }}
并使用以下代码:
$('#rubForm').submit(function(e){
e.preventDefault();
var $form = $( this ),
dataFrom = new FormData(document.forms.namedItem("myform"));
url = $form.attr( "action"),
method = $form.attr( "method" );
$.ajax({
url: url,
data: dataFrom,
type: method,
processData: false
});
});
答案 2 :(得分:1)
那是因为正在向数组发送&#34;数据&#34; ,与jquery ajax相同,Input::all()
显示[data]='_token=d76as78d6as87d6a&data1=value1
等...如果打印值Input::all
则不会作为sincronized请求显示完整的数组,laravel handle a jQuery发送的POST请求的不同方式