mafortis
mafortis

Reputation: 7128

Laravel ajax return 404

I'm trying to send data to back-end and i'm getting 404 error with this explanation in network tab:

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

Route

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

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

Controller

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

        //rest of data
    }

Blade

//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');
            }
          });
});

Any idea?

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

Upvotes: 0

Views: 46

Answers (1)

xmhafiz
xmhafiz

Reputation: 3538

if you are using url() function, you should use the {{ url('/snaptoken') }}.

But if you want to use the "name" from the "securepaymentnow", use route() function with this example {{ route('securepaymentnow', $theId) }}.

Both should works.

Refer Laravel NamedRoute for details.

Upvotes: 1

Related Questions