在JS中从控制器调用函数

时间:2018-05-07 16:18:16

标签: javascript php laravel

在下面的代码中,我试图在脚本运行后运行函数pay()。我在脚本中插入了一个ajax请求来处理pay()但是在函数getPaidSetup成功后,pay()没有运行。

我能以正确的方式做到吗?任何帮助表示赞赏。

 <script>
         document.addEventListener("DOMContentLoaded", function(event) {
      document.getElementById("submit").addEventListener("click", function(e) {

        getpaidSetup({
          PBFPubKey: PBFKey,
          customer_email: "eg.com",
          customer_firstname: "Temi",
          customer_lastname: "Adelewa",
          custom_description: "Pay Internet",
          amount: 2000,
          customer_phone: "2342423423",
          currency: "USD",
          country: "US",
          "txref": "MG-" + Date.now(),
          onclose: function() {},
          callback: function(response) {
            var flw_ref = response.tx.flwRef; // collect flwRef returned and pass to a                  server page to complete status check.
            console.log("This is the response returned after a charge", response);
            if (
              response.tx.chargeResponseCode == "00" ||
              response.tx.chargeResponseCode == "0"
            ) 
            {
              // redirect to a success page

            } 
            else 
            {
              // redirect to a failure page.
            }
          }
        });

     $.ajax({
        url: "/monthly_plan/basic",
        type: "post",
        data: {field_name :'data'}
     });

      });


    });



    </script>

控制器

 public function pay(Request $request)
    {
        $starter = new Payment(array(
            'plan_id' => 1,

        ));

        $starter->save();
    }

路线

Route::post('/monthly_plan/basic',['uses'=>'Controller@pay']);

0 个答案:

没有答案