从PHP页面将JSON数据作为Javascript变量传递 [英] Passing JSON data from a PHP page as a Javascript variable

查看:100
本文介绍了从PHP页面将JSON数据作为Javascript变量传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PayPal Checkout Express(客户端)javascript中间,我需要使用AJAX来调用PHP页面的输出,但是我有点卡住了.

In the middle of a PayPal Checkout Express (client-side) javascript, I need to use AJAX to call the output of a PHP page, but I'm a bit stuck.

PHP页面:

$data = array('retid' => $username);
header('Content-Type: application/json');
echo json_encode($data);

现在在另一个JavaScript页面中,我只想通过AJAX将PHP变量$username捕获为javascript变量.

Now inside the other javascript page I simply want to capture the PHP variable $username, via AJAX, as a javascript variable.

<?php
$IDToPassPlus = ($id.'&retid=');
?>


<script>

//It's the inclusion of this part, which tries to get the "retid" js variable, that stops the script from rendering the Paypal button:
$.ajax({
type: 'POST',
url: 'test-call.php',
dataType: 'json',
success: function(response) {
var retid = response.data.retid; 
},
});


paypal.Button.render({
env: 'sandbox',

client: {
sandbox:    'xxxxx',
production: 'xxxxx'
},

commit: true,

style: {
layout: 'vertical',
size:   'responsive',
shape:  'rect',
color:  'gold'
},

payment: function(data, actions) {
return actions.payment.create({
payment: {
transactions: [
{
amount: { total: '0.99', currency: 'GBP' }
}
],
redirect_urls: {
'cancel_url': 'pay-return-cancel.php?id=<?php echo $IDToPassPlus; ?>'+retid
}
}
});
},


onAuthorize: function(data, actions, error) {
return actions.payment.execute().then(function() {
window.alert('Payment Complete!');
window.location.replace('test-return.php?id=<?php echo $IDToPassPlus; ?>'+retid);
if (error === 'INSTRUMENT_DECLINED') {
actions.restart();
}
});
},

onCancel: function(data, actions) {
return actions.redirect();
},

onError: function(err) {
window.location.replace('pay-return-error.php?id=<?php echo $id; ?>'+retid);
}

}, '#paypal-button');


</script>

在没有贡献者的AJAX建议的情况下,该按钮可以正常使用.但是我试图通过AJAX从PHP页面传递一个变量,以将其添加到重定向中.

Without the contributor's AJAX suggestion, the button works perfectly. But I'm trying to pass a variable from a PHP page by way of AJAX, to add it onto the redirects.

推荐答案

首先,阅读整个页面;它确实会在您作为开发人员的整个职业生涯中为您提供帮助,它极大地帮助了我: https://stackoverflow.com/help/mcve

First, read this entire page; it will really help you throughout your career as a developer, it has helped me tremendously: https://stackoverflow.com/help/mcve

然后,让我们使用从该页面获得的知识来制作MCVE.将此放到新页面上:

Then, lets use the knowledge gained from that page to make an MCVE. Put this on a new page:

$.ajax({
    type: 'POST',
    url: 'test-call.php',
    dataType: 'json',
    success: function(response) {
        var retid = response.data.retid;
        console.log(retid);
    },
});

这应该将retid的值输出到您的控制台.看一下您的控制台.注意到任何错误,或者retid的值是否按预期打印?

This should print the value of retid to your console. Take a look at your console. Notice any errors, or does the value of retid print as expected?

那么为什么我们要花时间创建一个新页面并放在上面呢?我们正在缩小问题范围,我们正在尝试通过创建MCVE来查找问题的确切原因.如果我们不知道是什么原因引起的,或者如果我们无法创建一个非常基本的示例来说明问题,那么我们将很难解决问题和/或寻求帮助.

So why have we taken time to create a new page and put this on it? We are narrowing down our issue, we're trying to find the exact cause of the problem by creating an MCVE. If we don't know what is causing the problem, and if we can't create a very basic example to illustrate the problem, we will have a hard time solving the problem and/or asking for help.

(注1,将代码张贴在此处时,使代码漂亮".应缩进它的缩进;这使其他人更容易阅读.您在要求人们抽出时间来帮助您,这是免费的;使他们尽可能容易地阅读和理解您的代码)

(Note 1, make your code "pretty" when you post it here. Indent it as it should be indented; this makes it easier for others to read. You are asking people to take time out of their day to help you, for free; make it as easy as possible for them to read and understand your code)

(注2,这是一个示例,其中我遇到了一些非常非常复杂的MySQL交互.我没有发布所有复杂的代码,而是遵循了MCVE的概念:DRY-如何将重复的代码提取到...存储的函数中? 并提出了一些非常简单的伪造我的问题的例子.自从这样做以来,我能够从专家那里得到快速,简洁的答案,请注意,我接受的答案是所有参与者中得分最高的用户之一Stackoverflow)

(Note 2, here is an example of where I had some very, very complicated MySQL interactions that I had a question about. Rather than post all of the complicated code, I followed the MCVE concept: DRY - how to extract repeated code to...a stored function maybe? and made some fake, very very simplified examples of my problem. Since I did that, I was able to get quick, concise answers from experts, notice the answer I accepted was from one of the top-scored users on all of Stackoverflow)

这篇关于从PHP页面将JSON数据作为Javascript变量传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆