php 将数据从 AJAX 传递到Laravel中的控制器

35g0bw71  于 10个月前  发布在  PHP
关注(0)|答案(2)|浏览(67)
`<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>`

字符串
我有这个按钮包含所有的值,我想在控制器上显示,所以对于这第一次我调用 AJAX 存储值比我在控制器传递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>`

控制器

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


所有的价值,我可以看到完美 AJAX 调用,但当涉及到控制器,它不取请你能告诉我什么是错的吗
``

svgewumm

svgewumm1#

我不知道这对你是否有用,但对我有用

$data = json_decode(
    json_encode(
        $request->json()->all()
    )
);

字符串

0h4hbjxa

0h4hbjxa2#

我已经给你们举了一个很基本的例子。对我很有效

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

字符串

相关问题