【问题标题】:How to call a variable in post function如何在 post 函数中调用变量
【发布时间】:2018-12-24 10:13:32
【问题描述】:

我在 post 函数中调用了一个变量 (t_d_url)。它不显示可变数据。它只显示变量名。

我试过这样称呼它:

$.post(t_d_url.'/ascott/includes/booking/booking-summary.php', {}, function(data) {
    $(".booking-summary").html(data);
});

这里是变量:

var t_d_url = '<?php echo get_template_directory_uri(); ?>';

这是输出:

所以我想在这个函数中显示这个变量数据。

【问题讨论】:

  • 为什么不直接做$.post('<?php echo get_template_directory_uri(); ?>'+'/ascott/includes/booking/booking-summary.php',...
  • 后面看代码比较复杂..
  • 如果使用js可以不用'+'号代替'.'运算符
  • 是的...他正在使用 PHP 语法
  • 你应该学会在 WP 中正确使用 AJAX。只是为了确保您保持一切安全并正常工作。 codex.wordpress.org/AJAX_in_Plugins

标签: javascript php ajax wordpress


【解决方案1】:

您不能使用. 来合并两个字符串。这仅适用于 PHP。在Javascript中你应该使用+..

$.post(t_d_url+'/ascott/includes/booking/booking-summary.php', {}, function(data) {
    $(".booking-summary").html(data);
})

【讨论】:

  • 感谢@r-pelzer 删除这些标签。
  • 对不起,我弄错了 less url。现在,这正在起作用。谢谢你......只需将.更改为+
【解决方案2】:

您使用了不正确的连接运算符。 . 不适用于 Javascript 尝试使用 + as

let url = "<?php echo get_template_directory_uri(); ?>/ascott/includes/booking/booking-summary.php";
$.post(url, {}, function(data){  $(".booking-summary").html(data);}); 

在最坏的情况下,这将在当前域上调用/ascott/includes/booking/booking-summary.php

【讨论】:

    【解决方案3】:

    您可以使用以下两种方法中的任何一种。在 JavaScript 中使用+,因为JS 不使用点合并字符串(.):

    $.post(t_d_url+'/ascott/includes/booking/booking-summary.php', {}, function(data) {
                    $(".booking-summary").html(data);
                })
    

    使用PHP标签

    $.post('<?php echo get_template_directory_uri(); ?>'+'/ascott/includes/booking/booking-summary.php', {}, function(data) {
                        $(".booking-summary").html(data);
                    })
    

    【讨论】:

      猜你喜欢
      • 2015-10-25
      • 2022-10-04
      • 2021-07-25
      • 2021-12-21
      • 1970-01-01
      • 2021-10-25
      • 1970-01-01
      • 1970-01-01
      • 2015-06-12
      相关资源
      最近更新 更多