首页 > 解决方案 > 将 AJAX 函数调用到 REST(Paypal)部分中的 PHP 函数脚本,在 jQuery 中而不在 REST 中工作

问题描述

我在这里有点不知所措,困惑了几个小时。

在 Paypal REST 成功部分,我正在尝试调用一个函数。在将其移至 REST 部分之前,我使用常规按钮 onclick 事件进行了测试。工作得很好。

这里的代码:

        ...
onAuthorize: function(data, actions) {
        return actions.payment.execute()
            .then(function() {
                // window.alert('Payment Complete!');
                $.ajax({
                    type: "POST",
                    url: 'functions.php',
                    //dataType: "json",
                    data: {cuf_credits: '<?php echo $cuf_credits; ?>'},     
                    success: function(data){
                        console.log(data);
                        $("#output").text("Thanks for buying " + data.cuf_credits + " credits");
                    }
                });
                // Show a thank-you note
                document.querySelector('#paypal-button-container').style.display = 'none';
                document.querySelector('#thanks').style.display = 'block';
                document.querySelector('#orderdetails').style.display = 'none';         
            });
    }
...

查询选择器填写得很好,jQuery 成功部分中的一些基本文本也是如此。尽管它们返回“未定义”,但这很好,我只是想发送,而不是获取返回数据。

functions.php里面的函数没有被执行,但是当我在这段代码中使用它时,它是:

var testme = document.getElementById('test');
testme.onclick = function () {
    console.log('Test Started');
    $.ajax({
        type: "POST",
        url: 'functions.php',
                    data: {cuf_credits: '<?php echo $cuf_credits; ?>'},     
        success: function(data){
            console.log(data);
        }
    });

    function error(err) {
    console.log('Not Closed!');     

    }   
};

如果有人能指出我正确的方向,那就太棒了!

标签: javascriptphpjquerypaypal

解决方案


让我们尝试一些调试...

像这样修改你的AJAX块:

...
onAuthorize: function(data, actions) {
        return actions.payment.execute()
            .then(function() {
                // window.alert('Payment Complete!');
                //check to make sure your code is getting this far
                ////////start debug
                console.log("Calling AJAX...");
                ////////end debug
                $.ajax({
                    type: "POST",
                    url: 'functions.php',
                    //dataType: "json",
                    data: {cuf_credits: '<?php echo $cuf_credits; ?>'},     
                    success: function(data){
                        console.log(data);
                        $("#output").text("Thanks for buying " + data.cuf_credits + " credits");
                    }
                    //add a 'complete' function to get the status of the call
                    ////////start debug
                    complete: function(obj, textStatus){
                        console.log("AJAX Status: " + textStatus);
                    }
                    ////////end debug
                });
                // Show a thank-you note
                document.querySelector('#paypal-button-container').style.display = 'none';
                document.querySelector('#thanks').style.display = 'block';
                document.querySelector('#orderdetails').style.display = 'none';         
            });
    }
...

尝试此操作后,让我知道您的控制台日志是什么样的...


推荐阅读