简体   繁体   English

处理一些从 PHP 后端到 AJAX 的数据

[英]Processing some data from PHP backend to AJAX

I am working on a Laravel application whereby I am to reach out to a payment gateway and listen for payment status after 5 seconds for three consecutive time.我正在开发一个 Laravel 应用程序,我将在 5 秒后连续 3 次访问支付网关并监听支付状态。 I want to pass the data from the controller to AJAX code and use setInterval Js method to perform the function.to repeatedly reach out to the API for 3 consecutive times before stopping the transaction.我想将数据从控制器传递到 AJAX 代码并使用 setInterval Js 方法来执行该功能。在停止事务之前连续 3 次重复访问 API。 I have no idea of how to go around it..我不知道如何解决它..

Current controller containing PHP logic包含 PHP 逻辑的当前控制器

 public function payFinal(Request $request)
        {

             //Convert to a JSON object
            $data =(object)$request->all();

            $quote = $request->session()->get('quoteID');

             $all = array(
                'phone' => $data->phone,
                'quote_id' => $quote,
                'payment_type' => $data->type,
            );

            //Post data to the payment gateway
            $response = $this->global_Curl($all, 'api/payment/checkout');

            //Get some data from the response from the API
            $checkID = $response->data->CheckoutRequestID;

            //Get another set of data
            $type = $data->type;

            //Add the data in an array and store in a variable ready for processing
            $data = array(
                'payment_reference' => $checkID,
                'payment_type' => $type
            );

            //Reach out to another API to get status. This set of data is to be done repeatedly
            $payStat = $this->global_Curl($data, 'api/payment/status')->data;
            if ($payStat->status === 1) {
                    return 'true';
            }
        }

The code below is taken from the function above and I want to pass it to AJAX and use setInterval method to repeat it下面的代码取自上面的函数,我想将它传递给 AJAX 并使用 setInterval 方法重复它

~ kindly assist.. ~请帮忙..

//Reach out to another API to get status. This set of data is to be done repeatedly
                $payStat = $this->global_Curl($data, 'api/payment/status')->data;

AJAX code to fetch the data from从中获取数据的 AJAX 代码

$.ajax({  
    url: 'getquote',
    type: 'get',
    contentType: 'application/x-www-form-urlencoded',
    data: '',
    success: function success(response) {
        console.log(response);
    },
    error: function error(data) {
        console.log(data);
    }
});

The code given is not clear..!给出的代码不清楚..! However you can probably try like as follows.但是,您可以尝试如下。 I believe you are calling Controller's action as ajax url from javascript and the php function payFinal() is being called in that action then the following code will help you..!我相信您正在从 javascript 中将控制器的操作作为 ajax url 调用,并且在该操作中调用了 php 函数payFinal()那么以下代码将帮助您..!

JavaScript code: JavaScript 代码:

setInterval("AJAX Function to be invoked", 5000);

The above code call the ajax function for every 5 second.上面的代码每5秒调用一次ajax函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM