ajax不会发出http发布请求

时间:2019-05-16 13:04:09

标签: java ajax laravel

我在laravel 5.x上,我想调用带有某些参数的控制器,但是它什么也没做。

这是我的操作按钮:

<button class="button-details" data-idproject="{{ $project['id'] }}" data-fin="{{ $fin }}" data-debut="{{ $debut }}" data-iduser="{{$user['id']}}">+</button>

这是我的ajax代码:

         headers: {
             'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
         }
        });

        $(function() {
            $('table').tablesorter();
        });

        $(".button-details").click(function (e) {
            e.preventDefault();
                let user_id = $(this).data('iduser');
                let debut = $(this).data('debut');
                let fin = $(this).data('fin');
                let project_id = $(this).data('idproject');
            $.ajax( {
                url: '/details_project',
                type: 'POST',
                data: "user_id="+user_id,"project_id="+project_id,"debut="+debut,"fin="fin,
                success: function (data) {
                        console.log(data);
                    // $("."+id).hide();
                },
                error : function() {
                    console.log('ERROR');
                }
            });
        });```

1 个答案:

答案 0 :(得分:1)

将数据作为对象传递

    $(".button-details").click(function (e) {
        e.preventDefault();
            let user_id = $(this).data('iduser');
            let debut = $(this).data('debut');
            let fin = $(this).data('fin');
            let project_id = $(this).data('idproject');

            $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
        });

        $.ajax( {
            url: '/details_project',
            type: 'POST',
            data: {
                user_id: user_id,
                project_id: project_id,
                debut: debut,
                fin: fin
            },
            success: function (data) {
                    console.log(data);
            },
            error : function() {
                console.log('ERROR');
            }
        });
    });

在控制器中:

$post = $request->all();

//use data 
$user_id = $post['user_id'];
$project_id = $post['project_id'];
$debut = $post['debut'];
$fin = $post['fin'];