Receiving an ajax request with laravel

1

I'm having trouble retrieving the data sent by my ajax to laravel .

My ajax looks like this:

var id = $(this).siblings(':hidden').val(),
    qtd = $(this).val(),
    dataUser = { "id": id, "qtd": qtd};

 $.ajax({
    type: 'POST',
    data: {'rel':dataUser},
    url: window.location.href + "/atualiza",
    success: function (e) {
           alert(e);
    }
 });

And in my controller it looks like this:

public function atualizaQtd(){
    $arr = json_decode($_POST['rel'], true);
    return $arr;
}

And so my route:

Route::post('/carrinho/atualiza', [
   'uses' => 'CartController@atualizaQtd',
   'as' => 'carrinho.atualiza'
]);

I need to access the data of id and qtd la in my controller to perform an activity.

How do I do this? I tried to receive using the Requests method and I also could not.

  

When doing no error, it just does not return anything.

    
asked by anonymous 20.10.2016 / 02:16

1 answer

1

Has adjustment to be made for shipping no :

Within tag head of html need to have:

  

<meta name="csrf-token" content="{{ csrf_token() }}"> :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">        
    <script src="/js/jquery-2.2.4.min.js"></script>
    <title>Laravel</title>
    <meta name="csrf-token" content="{{ csrf_token() }}">
</head>

Because, request with verb POST has protection CSRF Protection is enabled by default. In the part of the needs an adjustment that is adding one header no $.ajaxSetup : X-CSRF-TOKEN .

<script>
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    function send(){
        var id  = $(this).siblings(':hidden').val();
        var qtd = $(this).val();
        var dataUser = { "id": id, "qtd": qtd};

        $.ajax({
            type: 'POST',
            data: {'rel':dataUser},
            url: '{{route("carrinho.atualiza")}}',
            success: function (e)
            {
                console.log(e);
            }
        });
    }
</script>

Note that you also had $.ajax modified url , putting route and calling the route that was created in the

20.10.2016 / 02:43