A
A
Andrey Boychenko2016-04-27 23:36:41
JavaScript
Andrey Boychenko, 2016-04-27 23:36:41

Ajax, Jquery and Laravel 5?

Goodnight! I'm learning laravel and I can't figure out what's the matter at all, the problem is that the controller method does not return anything, although I send the form to it.

{!! Form::open(['url'=>'#','files'=>true, 'method' => 'POST', 'id' => 'sendTest' ]) !!}
        {!! Form::text('name',null,['id'=>'name']) !!}
        {!! Form::text('age',null,['id'=>'age']) !!}
        {!! Form::submit('Click Me!',['id'=>'ok', ]) !!}
    {!! Form::close() !!}

<script>
    $(document).ready(function(){
        $.ajaxSetup({
            headers: {
                'X-XSRF-TOKEN': $('meta[name="_token"]').attr('content')
            }
        });

        $("#ok").on("click",(function(){
            var formData = new FormData(document.getElementById("sendTest"));
            $.ajax({
                url: 'create/',
                type: 'POST',
                data: formData,
                dataType: 'JSON',
                success: function (data) {
                    console.log(data);
                }
            });
        }));

    });
</script>

public function getRamos(Request $request)
    {
        $data = $request->all();
        //return $input;
        if($request->ajax()){
            return response()->json([ $data
            ]);
        }
    }


router
Route::get('articles/create','[email protected]');
Route::post('articles/create','[email protected]');


I'm definitely doing something wrong. Tell me what exactly? Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D', 2016-04-27
@Denormalization

Try adding:
'X-Requested-With': 'XMLHttpRequest' in headers

$.ajaxSetup({
            headers: {
                'X-XSRF-TOKEN': $('meta[name="_token"]').attr('content'),
                'X-Requested-With': 'XMLHttpRequest'
            }
        });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question