简体   繁体   English

在laravel5中使用jquery ajax上传图像的表单提交

[英]form submission with image upload using jquery ajax in laravel5

i am trying to submit a form with image upload using jquery ajax, but each time i select an image and click on submit button alert($(this).serialize()) shows- 我正在尝试使用jquery ajax提交带有图像上传的表单,但是每次我选择一个图像并单击提交按钮alert($(this).serialize())时都会显示-

_token=TCWpR3n9Uf2FpKMXi639Dcvzhc7t4fVDWDopjZ8V . _token = TCWpR3n9Uf2FpKMXi639Dcvzhc7t4fVDWDopjZ8V。

here is my form - 这是我的表格-

{!! Form::open(['action'=>'ImageController@store', '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() !!}

here the js- 这是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>

i want to get the serialized data of form and post via ajax to my controller. 我想获取表格的序列化数据并通过ajax发布到我的控制器。 why alert always shows the token? 为什么警报总是显示令牌?

In case of ajax image uploading "$(this).serialize()" didn't work. 如果是ajax图片,则无法上传“ $(this).serialize()”。

You have to pass (data:formData,) as below - 您必须通过以下(data: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){

        }
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM