2014-10-02 108 views
1

即时通过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 
      }); 
     }); 
+0

试试这个插件 http://blueimp.github.io/jQuery-File-Upload/ – justrohu 2014-10-02 10:06:51

+0

我不想上载一个插件,我只需要在只有这种形式上传,这样可以帮助我为什么这不起作用? – 2014-10-02 10:20:14

回答

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 
    }); 
}); 
+0

我不认为你已经阅读我的问题描述,我已经这样做了,但$ form.serialize不适用于文件! – 2014-10-02 10:59:04

+0

@Ilyass我编辑了我的答案 – 2014-10-02 11:09:56

+0

谢谢sooooo多!!你救了我的命!!但是如果你知道为什么正常的选择器不起作用?什么与document.forms? – 2014-10-02 12:22:37

1

这是因为在发送与“数据”的排列,相同jQuery的阿贾克斯,在Input::all()正显示出[data]='_token=d76as78d6as87d6a&data1=value1等..不作为sincronized要求,如果你首席t值Input::all会告诉你一个完整的数组,laravel处理jQuery发送的POST请求的不同方式

2

关键在你的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('[email protected]') }}', // you need change it. 
    data: formdata, // high importance! 
    processData: false, // high importance! 
    success: function (data) { 

     //do thing with data.... 

    }, 
    timeout: 10000 
});