palla451
4 months ago

How to pass the route in url parameter in jquery ajax call

Posted 4 months ago by palla451

Hi all, i have a route in web.php


Route::middleware('auth')
    ->prefix('dashboard')->group(function(){
        Route::post('ajax_input_store','[email protected]')->name('ajax_input_store');
});

In my controller


   public function ajaxInputStore(Request $request)
    {
        $user = Auth::user();

        $myDateTime = DateTime::createFromFormat('d-m-Y', $request->get('date'));
        $newDateString = $myDateTime->format('Y-m-d H:i');


        $input = new Input([
            'user_id' => $user->getAttribute('id'),
            'description' => $request->get('description'),
            'import' => $request->get('importo'),
            'date' =>  $newDateString
        ]);

        $input->save();
        $input->payments()->attach($request->get('payment'),[
            'paymentable_id' => $input->getAttribute('id'),
            'paymentable_type' => 'App\Models\Input']);

        return response()->json(['success'=>'Form Data is successfully Stored']);

    }

dafields.js


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

    $("#addInput").click(function(e) {
        e.preventDefault();

        var description = $("#description").val();
        var importo = $("#import").val();
        var payment = $("#payment").val();
        var date = $("#datepicker").val();

        $.ajax({
            type:'POST',
            data: {
                description: description,
                importo: importo,
                payment: payment,
                date : date
            },
            url:"{{ route('ajax_input_store') }}",
            // url: '/dashboard/ajax_input_store',
            success:function(data) {
                $('#formNewInput').modal("toggle");
                alert('save correctly!...');
                resetMyForm();
            },
            error: function(){
                alert("wrong...!!!");
            }
        });
    });
});


// Form reset function
function resetMyForm(){
    $("#description").val('');
    $("#import").val('');
    $("#payment").val(1);
    $("#datepicker").val('');
}

but i have error because don't read the route correctly.

If i insert


url:"/dashboard/ajax_input_store",

the function correctly and have response 200

Please sign in or create an account to participate in this conversation.