Laravel Ajax返回404

时间:2018-10-09 07:57:45

标签: ajax laravel

我正在尝试将数据发送到后端,并且在“网络”标签中收到以下说明导致404错误:

"message": "",
"exception": "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException",

路线

Route::middleware('verified')->group(function () {

   Route::post('/snaptoken/{id}', 'Admin\PayController@token')->name('securepaymentnow');
});

控制器

public function token(Request $request, $id) 
    {
        //Find project
        $project = Project::findOrFail($id);

        //rest of data
    }

刀片

//form and button
  <form id="payment-form" method="POST" action="{{route('securepaymentnow', $project->id)}}">
    @csrf
    <input type="hidden" name="result_type" id="result-type" value="">
    <input type="hidden" name="result_data" id="result-data" value="">
  </form>
  <button class="btn-sm bg-success pay-button" data-id="{{$project->id}}" type="submit"><i class="fas fa-fas fa-shield-alt"></i> Secure Payment</button>

//javascript

$('.pay-button').click(function (event) {
        $.ajaxSetup({
            headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') }
        });
        event.preventDefault();
        // $(this).attr("disabled", "disabled");
        var prdfoId = $(this).data('id');
          $.ajax({
            url: '{{url("/securepaymentnow")}}/'+encodeURI(prdfoId),
            type: "POST",
            cache: false,

            success: function(data) {
              var resultType = document.getElementById('result-type');
              var resultData = document.getElementById('result-data');
            }
          });
});

有什么主意吗?

............................................... ................................................... ........................

1 个答案:

答案 0 :(得分:1)

如果您使用的是url()函数,则应该使用{{ url('/snaptoken') }}

但是,如果您想使用“ securepaymentnow”中的“名称”,请在此示例route()中使用{{ route('securepaymentnow', $theId) }}函数。

两者都应该起作用。

有关详细信息,请参见Laravel NamedRoute

相关问题