【问题标题】:Javascript Variable to Php json decodedJavascript 变量到 PHP json 解码
【发布时间】:2014-09-11 00:59:44
【问题描述】:

其实我是用这个脚本来获取instagram用户头像的

<script src="http://codeorigin.jquery.com/jquery-2.0.3.min.js"></script>
  <script type="text/javascript">
var user_id = <? echo $page_id; ?>;
var url = "https://api.instagram.com/v1/users/"+user_id+"?client_id=775829274b6f48c1ac2bf218dda60e16&callback=?";
$.getJSON(url, function(data) {
    $("body").append("<img src='"+data.data.profile_picture+"' />");

}
);

  </script>

这个脚本的输出是我需要的 100%。 我现在需要的只是将图像链接到一个 PHP 变量,即 (+data.data.profile_picture+)

如果尝试使用 + ?link="+data.data.profile_picture+" 重新加载同一页面 所以我可以用 $_GET['link'] 抓住它 像这样:

window.location.href = location.href + '?link="+data.data.profile_picture+";

但它没有.. 我应该怎么做才能将此 javascript 变量传递给 PHP 变量。

【问题讨论】:

    标签: javascript php json href


    【解决方案1】:

    试试这个

    window.location.href = location.href + '?link='+data.data.profile_picture;
    

    【讨论】:

    • 如果不使用if语句检查$_GET['link']是否已经设置,这将导致无限循环
    【解决方案2】:

    您需要将数据发送到 PHP,最简单的方法是使用jQuery.ajax()

    var picture = data.data.profile_picture;
    $("body").append("<img src='"+picture+"' />");
    $.ajax({
                url: "http://example.com/webservices/getProfilePicture.php",
                type: "POST",
                data: {
                    'profile_picture': picture
                },
                success: function () {
                    console.log('Success!');
                    // Place eventual post-treatment here
                },
                error: function () {
                     console.log('Error..');
                     // Place eventual error-handling / debugging here
                }
            }
    );
    

    });

    然后在 PHP 中你可以用这个来访问它:

    <?php $profile_picture = $_REQUEST['profile_picture']; ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-06
      • 1970-01-01
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      • 2012-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多