Laravel ajax-此路由不支持POST方法。支持的方法:GET,HEAD

时间:2020-10-27 18:42:54

标签: javascript laravel

我试图在laravel中创建一个ajax,我得到了这样的消息:“此路由不支持POST方法。受支持的方法:GET,HEAD”。问题是我之前在项目中做过ajax,只有这样我才有问题。有人可以帮忙吗?

我的路线:

Route::get('/admin/clients-data','App\Http\Controllers\ClientsController@gravy');
Route::get('/admin/client-details/{slug}','App\Http\Controllers\ClientsController@clientdetails');
Route::post('/admin/client-details/adddocumentstoclient','App\Http\Controllers\ClientsController@adddocumentstoclient');

我的控制器:

public function adddocumentstoclient(Request $request){
        echo '<pre>';
        var_dump('bump');
        echo '</pre>';
        die();
    }

还有我的js:

$('.tasks input').on('click', function() {
        var emptyValue = 0;
        $('input:checked').each(function() {
            emptyValue += parseInt($(this).val());
        });
        var documents_brought = [];
        var documents = $('.documents_brought input:checked').each(function(){
            documents_brought.push($(this).data('documentid'));
        });
        var client_id = $('.documents_brought').data('clientid');
        $.ajax({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            type: "POST",
            url: "adddocumentstoclient",
            data: {'documents_brought':documents_brought,'client_id': client_id},
            success: function (html) {
                // returndata =  JSON.parse(html);;
                // toastr.success(returndata.message, returndata.title);
            }
        });
        $('.progress-bar').css('width', emptyValue + '%').attr('aria-valuenow', emptyValue);
    });

预先感谢

1 个答案:

答案 0 :(得分:0)

好,问题出在这里

Route::get('/admin/client-details/{slug}','App\Http\Controllers\ClientsController@clientdetails');
Route::post('/admin/client-details/adddocumentstoclient','App\Http\Controllers\ClientsController@adddocumentstoclient');

第一条路线是get,并且您接受slug参数,因此,每当您点击/admin/client-details/adddocumentstoclient时,它将与第一条路线get相匹配,并且您将永远无法到达第二条路线。

此处的解决方案是在slug/admin/client-details/{slug}之前添加前缀,或更改第二条路由前缀/admin/client-details/以区分两条路由。