javascript - 将 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!');
}
};
如果有人能指出我正确的方向,那就太棒了!
解决方案
让我们尝试一些调试...
像这样修改你的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';
});
}
...
尝试此操作后,让我知道您的控制台日志是什么样的...
推荐阅读
- sql - Delphi:更新 SQLite 记录的多个字段
- python-3.x - Python 类继承
- php - PHP | left join avg query 给我 Array()
- node.js - 将 express 后端连接到 React 前端(如果可能,在同一台服务器上)
- ios - 如何在 Swift 中设置属性本身可变的属性值
- mysql - 如何从用户在 django 基本模板中创建的所有帖子中获取模型字段值的总和?
- flutter - 如何使用 ListViews 实现从退出和关闭 Flutter 应用程序的平滑过渡
- java - Java Spring-boot:如何返回列表而不是单个结果?
- python - 带有输入的图形用户界面
- c - 如何检查文件中是否没有内容,以及如何避免从文件中读取最后一个字符?