如何在post函数中调用变量

时间:2018-12-24 10:13:32

标签: javascript php ajax wordpress

我在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(); ?>';

以下是输出:

This is Error Result

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

3 个答案:

答案 0 :(得分:1)

您不能使用.合并2个字符串。那只适用于PHP。在Javascript中,您应该使用+ ..

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

答案 1 :(得分:0)

您使用了不正确的隐藏运算符。 .Javascript不兼容,尝试将+用作

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

答案 2 :(得分:0)

您可以使用以下两种方法中的任何一种。在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);
                })
相关问题