gpt4 book ai didi

php - 在 html 上写入 facebook 数据

转载 作者:行者123 更新时间:2023-12-02 18:29:31 25 4
gpt4 key购买 nike

我正在从 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>

最佳答案

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

您可以执行诸如 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>";
}

输出

  • toast
  • 热巧克力!

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

关于php - 在 html 上写入 facebook 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17970019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com