0

我正在从 facebook 中检索可以在控制台上显示但未能在 html 页面上显示的数据。

我试过:

$("#mydiv").text(data);

或者

$("#mydiv").append(data);

[object Object]在浏览器上显示。

我的代码是:

<?php
session_start();
$appId = 'APPID'; 
$appSecret = 'APPSECRET'; // Facebook App Secret
$return_url = 'dev01.dev/FB/facebook-php-sdk/examples/';  //path to script folder
//$fbPermissions = 'publish_stream,email'; //more permissions : https://developers.facebook.com/docs/authentication/permissions/
?>
<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml" xml:lang="en-gb" lang="en-gb" ><head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<title>JS/Ajax Facebook Connect</title>
 <script>
 function AjaxResponse()
 {
    var myData = 'connect=1'; 
     $.ajax({
    type: "POST",
    url: "process_facebook.php",
    data: myData
    }).done(function(result) {
    $("#fb-root").html(result);
    });
   }

function LodingAnimate() //Show loading Image
{
    $("#LoginButton").hide(); //hide login button once user authorize the application
}
 </script></head><body>
<?php
if(!isset($_SESSION['logged_in']))
{
?>
    <div id="results">    </div>
    <div class="results1">    </div>
    <div id="LoginButton">
    <button type="button" onclick="CallAfterLogin()" ?>Click Me!</button> 
    </div>
<?php
}
?>
<div id="fb-root"></div>
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
    appId: '<?php echo $appId; ?>',
    cookie: true,
    xfbml: true,
    channelUrl: '<?php echo $return_url; ?>channel.php',
    oauth: true});};

(function() {
    var e = document.createElement('script');
    e.async = true;
    e.src = document.location.protocol +'//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);}());

function CallAfterLogin(){
        FB.login(function(response) {  //---
        if (response.status === "connected")
        {
            LodingAnimate(); //Animate login
//            FB.api('/me?fields=movies,email', function(data) { //--
            FB.api('/me', function(data) { //--
            $("#results").text(data);
            console.log(data);
              if(data.email == null)
              {
                 alert("You must allow us to access your email id!");
                 ResetAnimate();

              }else{
                    AjaxResponse();
                   } 
              }); //--
         } //if
             }); //---
          } //main function
       <div id="results">    </div>
</script>
</body>
</html>
4

1 回答 1

2

您必须解析/循环对象以获取所需的数据

你可以做一些事情data.FirstName来获取FirstName财产的价值。(我不知道对象包含什么,这只是一个例子)。

JSFiddle

这是一个例子:

HTML

<ul id="output"></ul>

JavaScript

/* I just created a random object */
var myObject = {toaster: 'toasts',
                chocolate: 'hot, chocolate!'};

/* I get the DOMElement to dump data within it */
var output = document.getElementById('output');

/* I loop through each property of the object and add them to my DOMElement container */
for (var prop in myObject)
{
    output.innerHTML += "<li>"+ myObject[prop] +"</li>";
}

输出

  • 敬酒
  • 热可可!

另外:.innerHTMLvs .appendChild()vs $.append()

于 2013-07-31T12:13:27.210 回答