【问题标题】:Trying to pass the var value from the function to the AJAX url试图将 var 值从函数传递到 AJAX url
【发布时间】:2019-12-18 19:25:38
【问题描述】:

我正在尝试将 var 值从函数传递到 AJAX URL

function gonative_onesignal_info(info) {
    //var pushid = info.installationId;
    //var subscribed = info.oneSignalSubscribed;

    // EXAMPLE
    var subscribed = true;
    var pushid = "123456789";
}

if (subscribed === true) {
    $.ajax({ url: "ajax.php?push=" + pushid + "&key=<? echo $userkey; ?>",
        context: document.body,
        success: function(){
        }});
}

只有 php 值对我有用,但在控制台日志中显示如下:ajax.php?push=" + pushid + " 而不是 ajax.php?push=123456789

【问题讨论】:

  • 你为什么在你的函数之外调用 $.ajax ?有什么特别的原因吗?
  • 您可以将pushid 定义为(全局变量声明)[stackoverflow.com/a/5786899/6656706] 或将ajax 代码放入函数中。

标签: javascript ajax function var


【解决方案1】:
<?php echo $userkey; ?> or <?=$userkey; ?>

再试一次,这样你就可以得到..

【讨论】:

    【解决方案2】:

    因为 pushid 是在函数内部声明的,并且仅限于该范围。如果要使用该值,则必须将 ajax 请求函数放在 gonative_onesignal_info 中。查找本地范围与全局范围以及 var 和 let 关键字的使用

    【讨论】:

    • 我试图将 ajax 放入函数中,但 ajax url 中没有显示 var 值“pushid”
    猜你喜欢
    • 1970-01-01
    • 2020-05-03
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    相关资源
    最近更新 更多