0

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-

_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-

<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. why alert always shows the token?

2
  • So what data should you get on form serialize? Commented Dec 13, 2015 at 10:42
  • Here in your code their is no form element then token. If you want to upload img then stackoverflow.com/questions/19447435/… this should be the code. Commented Dec 13, 2015 at 10:46

1 Answer 1

1

In case of ajax image uploading "$(this).serialize()" didn't work.

You have to pass (data:formData,) as below -

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

        }
    })
Sign up to request clarification or add additional context in comments.

2 Comments

yes, this works! but how to retrieve value of formData in laravel controller?
this will pass value same as serialize() passes ,you can see the output using method Input::all();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.