skip to Main Content
`<button type="button" class="btn btn-primary refund-initiate-btn"
                                    data-prn="{{ $item->PRN }}"
                                    data-rpptxnid="{{ $item->RPPTxnId }}"
                                    data-amount="{{ $item->AMOUNT }}"
                                    style="width: max-content;">Refund Initiate</button>`

I have this button contain all the value which i want to show on controller so for this first i call a ajax to store value than i pass in controller
AJAX

` <script>
            $(document).ready(function() {
                // Function to handle the "Refund Initiate" button click
                $(".refund-initiate-btn").on("click", function() {
                    var prn = $(this).data("prn");
                    var rpptxnid = $(this).data("rpptxnid");
                    var amount = $(this).data("amount");
        
                    // Create an object with the data to be sent
                    var requestData = {
                        prn: prn,
                        rpptxnid: rpptxnid,
                        amount: amount,
                    };
                    console.log(requestData);
                    // Make the API call using AJAX
                    $.ajax({
                        url: "{{ route('refundlog') }}",
                        type: "POST",
                        data: JSON.stringify(requestData), // Convert data to JSON format
                        contentType: "application/json", // Set content type to JSON
                        dataType: "json",
                        beforeSend: function(xhr) {
                            xhr.setRequestHeader('X-CSRF-TOKEN', "{{ csrf_token() }}"); // Set CSRF token header
                        },
                        success: function(response) {
                            // Handle the API response here
                            console.log(response);
                            // You can process the response here or display it on the page
                        },
                        error: function(xhr, status, error) {
                            console.error(error);
                            // Handle API error here if any
                        }
                    });
                });
            });
        </script>`

Controller

` $prn = $request->input('prn');
        $rpptxnid = $request->input('rpptxnid');
        $amount = $request->input('amount'); `

all the value i can see perfectly in ajax calling but when it comes to controller it not fetch please can you tell me what is wrong in it

2

Answers


  1. I don’t know if it will work for you, but this works for me

    $data = json_decode(
        json_encode(
            $request->json()->all()
        )
    );
    
    Login or Signup to reply.
  2. I’ve made very basic example for you. It works for me.

    Route::post('/refundlog', [ProfileController::class, 'refund'])->name('profile.refundLog');
    
    ----------------------------------------------------
    
    public function refund(Request $request): array
    {
        return [
            "prn" => $request->prn,
            "rpptxnid" => $request->rpptxnid,
            "amount" => $request->amount
        ];
    }
    
    ----------------------------------------------------
    
    $(document).ready(function() {
        $("#btn").on("click", function() {
            var requestData = {
                prn: "aksasd6a54sd6a5s",
                rpptxnid: 123,
                amount: 50.00,
            };
            console.log(requestData);
    
            // Make the API call using AJAX
            $.ajax({
                url: "{{ route('profile.refundLog') }}",
                type: "POST",
                data: JSON.stringify(requestData), // Convert data to JSON format
                contentType: "application/json", // Set content type to JSON
                dataType: "json",
                beforeSend: function(xhr) {
                    xhr.setRequestHeader('X-CSRF-TOKEN', "{{ csrf_token() }}"); // Set CSRF token header
                },
                success: function(response) {
                    // Handle the API response here
                    console.log(response);
                    // You can process the response here or display it on the page
                },
                error: function(xhr, status, error) {
                    console.error(error);
                    // Handle API error here if any
                }
            });
        });
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search