2015-12-13 115 views
0

我想提交图像上传使用jQuery的Ajax,但每次我选择一个图像,然后单击提交按钮提醒($(this).serialize())显示 -表单提交与图像上传使用jQuery的ajax在laravel5

_token = TCWpR3n9Uf2FpKMXi639Dcvzhc7t4fVDWDopjZ8V。

这里是我的形式 -

{!! Form::open(['action'=>'[email protected]', 'name'=>'form1', 'id'=>'formId1', 'files'=>true]) !!} 

<div class="form-group"> 
{!! Form::file('image') !!}       
</div> 
<div class="form-group"> 
{!! Form::submit('Upload', array('class'=>'btn btn-danger', 'name'=>'image_form_submit' )) !!}       
</div> 
{!! Form::close() !!} 

这里js-

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> 

<script type="text/javascript"> 
    $(document).ready(function() { 
     $("form").submit(function(e){ // 
      e.preventDefault(); 


     alert($(this).serialize()); //always alerts the tocken- _token=TCWpR3n9Uf2FpKMXi639Dcvzhc7t4fVDWDopjZ8V 
     $.ajax({ 

      type:"POST", 
      url: $(this).attr('action'), 
      data: $(this).serialize(), 
      dataType: 'json', 
      success: function(data){ 
       console.log(data); 
      }, 
      error: function(data){ 

      } 
     }) 
      return false; 
     }); 
    }); 
</script> 

我想通过Ajax获得的形式和岗位的序列化数据到我的控制器。 为什么警报总是显示令牌?

+0

那么,什么数据,你应该得到的形式连载? –

+0

在你的代码中,它们不是表单元素,而是标记。如果你想上传img,那么http://stackoverflow.com/questions/19447435/ajax-upload-image?answertab=active#tab-top这应该是代码。 –

回答

1

如果ajax图像上传“$(this).serialize()”不起作用。

你要通过(数据:FORMDATA)如下 -

$.ajax({ 
     type:"POST", 
     url: $(this).attr('action'), 
     data:formData, 
     cache:false, 
     contentType: false, 
     processData: false, 
     success: function(data){ 
      console.log(data); 
     }, 
     error: function(data){ 

     } 
    }) 
+0

是的,这个工程!但如何在laravel控制器中检索formData的值? –

+0

这将传递与serialize()相同的值,您可以使用方法 Input :: all(); –