如何声明一个PHP变量并将其传递给AJAX url?

时间:2019-05-08 06:08:16

标签: javascript php ajax

当我致电/comment.php时,它将返回我已请求的数据。固定 post_id 的位置。

/comment.php

 "ajax":
{
 "url": "http://localhost/fb-callback.php?post_id=12345_67890",
   "type" : "GET",
"dataSrc":  function (response) { 
                // console.log(response);
                            if(response.whaterver == 0){
                               //DO YOUR THING HERE
                            }
                            //return back the response
                            return response; 
                        },

我想在那里声明一个php变量,而不是固定ID( 12345_67890 ), 像这样的东西::-

<?php
$uid = $_GET['uid'];
?>

 "ajax":
{
 "url": "http://localhost/fb-callback.php?post_id="+"&uid",
"type" : "GET",
 ................

这样,当我调用网址/comment.php?uid=12345_67890**时,它将返回结果。但是看来这行不通。 我该怎么办?或者还有其他方法可以打电话吗?

以下是完整代码::: https://pastebin.com/iy3htv63

2 个答案:

答案 0 :(得分:3)

您可以echo在ajax网址上。试试这个

 <?php
  $uid = $_GET['uid'];
?>

 "ajax":
{
 "url": "http://localhost/fb-callback.php?post_id=<?php echo $uid; ?>",
 "type" : "GET",
 ................

答案 1 :(得分:1)

尝试一下

<?php
$uid = $_GET['uid'];
?>
var uid = <?php echo $uid; ?>;
 "ajax":
{
 "url": "http://localhost/fb-callback.php?post_id="+uid,
"type" : "GET",
 ................