Receiving an ajax request with Laravel

Asked

Viewed 1,254 times

2

I am having problems in recovering the data sent by my ajax to the laravel.

Man ajax is 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 is 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 from id and qtd It was in my controller to perform an activity.

How do I do this ? I tried to receive using the method Requests and I also couldn’t.

By doing no error, just returns nothing.

1 answer

2


Has adjustment to be made for shipping in the :

Within the 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, requisition with verb POST has the protection CSRF Protection active by default. In the needs an adjustment that is the addition of a header in the $.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 also had in the $.ajax modified the url, putting route and calling the route that was created in the . It was all created within one function by name send().

In the controller can and should use Request:

class CartController extends Controller
{
    public function atualizaQtd(Request $request)
    {
        $rel = $request->get('rel');
        $id  = $rel['id'];
        $qtd = $rel['qtd'];
        return $id.'-'.$qtd;
    }
}

with these adjustments and observations will work the request and the values will be available.

  • Not picking up the request I have to put the function where ?

  • It is worth remembering that what moved this is the event of changing a field to Qtd, the idea is that whenever change the amount it goes to the bank and changes

  • @Renanrodrigues to change it needs to be called, do not have this information in your question!

  • @Renanrodrigues in your question you did not put also what you are doing, the answer is already a solution on what posted!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.