将 PHP var 从远程 PHP 脚本获取到 JQuery 中


Get PHP var from remote PHP script into JQuery

我已经尝试了大约 3 个小时,尝试了大量不同的方法,但我显然做错了什么,似乎无法理解。

我有一个 php 文件,它从变量 $tweeturl 中的另一个 php 脚本生成一个 url:

推特.php

<?php
$ch = curl_init("http://path/to/data.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$doit = curl_exec($ch);
$dom = new DOMDocument();
@$dom->loadHTML($doit);
$show = $dom->getElementById("showmeta");
$liveshow = $show->nodeValue;
$dj = $dom->getElementById("djmeta");
$livedj = $dj->nodeValue;
$npshow = $liveshow;
$npshow = preg_replace('/'s+/', '+', $npshow);
$npdj = $livedj;
$npdj = preg_replace('/'s+/', '+', $npdj);
$url1 = "https://twitter.com/intent/tweet?source=webclient&text=Listening+to+";
$url2 = $npshow;
$url3 = $npdj;

$tweeturl .= $url1;
$tweeturl .= $url2;
$tweeturl .= $url3;
?>

这已经过测试并且工作正常。

我正在尝试通过 AJAX 将变量放入 JQuery 以填充空白浏览器窗口的 url(我使用的是 wordpress,这就是函数被包装的原因(:

.HTML

<a class="player-twitter" href="#"</a>

杰奎里

<script type="text/javascript">
(function($) {
    $(document).ready(function() {
        $( "a.player-twitter" ).click(function( event ) {
            $.ajax({
                type: "GET",
                url: "http://path/to/tweet.php",
                data: { var: $tweeturl },
                success: function(e) {
                    window.open(<?php echo $tweeturl; ?>);
                }
            });
        })
    });
})( jQuery );
</script>
我知道我做错了

,但我就是想不通我做错了什么。

任何帮助将不胜感激。

编辑以显示在此处收到帮助后的工作脚本

尽管所有答案都是正确的,但我在这里不需要的 JSON 转换很多问题都过火了。我真的很感激,因为我从中学到的东西比我预期的要多。但是,如果像我一样,您只想从您的 php 脚本中获取一个 var,这就是应该这样做的方式......

推文.php(与原始问题相同,但在末尾添加了以下回声(

echo $tweeturl;

在我的天真中,我只是希望JQuery能够接受$tweeturl变量,而不必实际回声。 嘟!

杰奎里

<script type="text/javascript">
(function($) {$(document).ready(function() {
$( "a.player-twitter" ).click(function( event ) {
$.ajax({
type: "GET",
url: "http://path/to/tweet.php",
success: function ( response_string ) {
    window.open( response_string );
}
});
})
});})( jQuery );
</script>

再次感谢你们所有的帮助,不仅帮助我完成这项工作,还解释了我对JQuery的一些不了解的事情。

现在这里有一些使用 JSON 的答案。 但这在您的示例中是不必要的,因为您只想返回一个字符串。 所以这里有一个简单的解决方案:

在你的PHP中,你必须输出你想要返回给你的ajax调用的内容。 所以只需在 PHP 文件的末尾添加它:

echo $tweeturl;


在你的JavaScript中,改变你的Ajax调用:

$.ajax({
    type: "GET",
    url: "http://path/to/tweet.php",
    success: function ( response_string ) {
        window.open( response_string );
    }
});

就是这样 - 当您不向 PHP 文件发送任何数据时,您甚至不需要在这里data:..

你只需要在生成$tweet文件后的最后回显你的推文.php文件中的。一旦你从 ajax 调用 tweet.php,代码将执行,回显将作为函数的参数发送给你 ajax 成功。

法典:

推特.php

<?php
$ch = curl_init("http://path/to/data.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$doit = curl_exec($ch);
$dom = new DOMDocument();
@$dom->loadHTML($doit);
$show = $dom->getElementById("showmeta");
$liveshow = $show->nodeValue;
$dj = $dom->getElementById("djmeta");
$livedj = $dj->nodeValue;
$npshow = $liveshow;
$npshow = preg_replace('/'s+/', '+', $npshow);
$npdj = $livedj;
$npdj = preg_replace('/'s+/', '+', $npdj);
$url1 = "https://twitter.com/intent/tweet?source=webclient&text=Listening+to+";
$url2 = $npshow;
$url3 = $npdj;

$tweeturl .= $url1;
$tweeturl .= $url2;
$tweeturl .= $url3;
$url = array("url"=>$tweeturl);
//echo the tweet url back to your ajax
echo json_encode($url);
?>

阿贾克斯

<script type="text/javascript">
(function($) {$(document).ready(function() {
  $( "a.player-twitter" ).click(function( event ) {
    $.ajax({
      type: "GET",
      url: "http://path/to/tweet.php",
      success: function(tweeturl){
        /* "tweeturl" is the JSON object you receive from the tweet.php file */
        /* "url" is the name of your array key in the tweet.php file */
        var obj = jQuery.parseJSON(tweeturl);
        window.open(obj.url);
      }
    });
  })
});})( jQuery );
</script>

由于您没有在这段代码中使用任何参数,因此您需要做的就是通过在末尾添加echo来传回生成的 tweeturl。

<?php
$ch = curl_init("http://path/to/data.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$doit = curl_exec($ch);
$dom = new DOMDocument();
@$dom->loadHTML($doit);
$show = $dom->getElementById("showmeta");
$liveshow = $show->nodeValue;
$dj = $dom->getElementById("djmeta");
$livedj = $dj->nodeValue;
$npshow = $liveshow;
$npshow = preg_replace('/'s+/', '+', $npshow);
$npdj = $livedj;
$npdj = preg_replace('/'s+/', '+', $npdj);
$url1 = "https://twitter.com/intent/tweet?source=webclient&text=Listening+to+";
$url2 = $npshow;
$url3 = $npdj;

$tweeturl .= $url1;
$tweeturl .= $url2;
$tweeturl .= $url3;
// ADDED CODE TO RETURN THE RESULT OF ALL THIS PROCESSING
// but do it in a JSON way
$result = new stdClass();
$result->tweetUrl = $tweeturl;
echo json_encode($result);
?>

现在在 javascript 中,您将从 ajax 调用将数据作为字符串返回给您,该字符串可以转换为 json 对象,因此

<script type="text/javascript">
(function($) {
    $(document).ready(function() {
        $( "a.player-twitter" ).click(function( event ) {
            $.ajax({
            type: "GET",
            url: "http://path/to/tweet.php",
            // this was not used in the PHP so dont need to pass it as a parameter
            //data: { var: $tweeturl },
            success: function(data){
               result = $parseJSON(data); // convert json string to json object
               window.open(result.tweetUrl);
               }
          });
      })
   });
})( jQuery );
</script>