gpt4 book ai didi

javascript - Facebook 使用 jquery 移动框架

转载 作者:行者123 更新时间:2023-11-28 21:15:32 25 4
gpt4 key购买 nike

我正在尝试实现 facebook 的基本功能,例如更新 facebook 个人资料,以及使用 jquerymobile 框架进行 facebook 登录。使用此链接 http://thinkdiff.net/facebook/graph-api-javascript-base-facebook-connect-tutorial/

它适用于 HTML 5 格式。但是,当我与 JqueryMobile 集成时,我收到一条错误,提示未捕获异常:无法调用方法appendChild。

我将在此处粘贴代码:请查看并让我知道问题所在。

<body> 
<script type="text/javascript">
window.fbAsyncInit = function() {

FB.init({appId: 'xxxxxxxxxxxxxx', status: true, cookie: true, xfbml: true});



/* All the events registered */

FB.Event.subscribe('auth.login', function(response) {

// do something with response

login();

});

FB.Event.subscribe('auth.logout', function(response) {

// do something with response

logout();

});



FB.getLoginStatus(function(response) {

if (response.session) {

// logged in and connected user, someone you know

login();

}

});

};

(function() {

var e = document.createElement('script');

e.type = 'text/javascript';

e.src = document.location.protocol +

'//connect.facebook.net/en_US/all.js';

e.async = true;

document.getElementById('fb-root').appendChild(e); //i am getting error in the is line

});



function login(){

FB.api('/me', function(response) {

document.getElementById('login').style.display = "block";

document.getElementById('login').innerHTML = response.name + " succsessfully logged in!";

});

}

function logout(){

document.getElementById('login').style.display = "none";

}



//stream publish method

function streamPublish(name, description, hrefTitle, hrefLink, userPrompt){

FB.ui(

{

method: 'stream.publish',

message: '',

attachment: {

name: name,

caption: '',

description: (description),

href: hrefLink

},

action_links: [

{ text: hrefTitle, href: hrefLink }

],

user_prompt_message: userPrompt

},

function(response) {



});



}

function showStream(){

FB.api('/me', function(response) {

//console.log(response.id);

streamPublish(response.name, 'Something ', 'hrefTitle', 'http://www.ffff.com', "Share www.ffffff.com");

});

}



function share(){

var share = {

method: 'stream.share',

u: 'http://www.fffffff.com'

};



FB.ui(share, function(response) { console.log(response); });

}



function graphStreamPublish(){

var body = 'hsdfkjasdkjfadkjf;adlfj';

FB.api('/me/feed', 'post', { message: body }, function(response) {

if (!response || response.error) {

alert('Error occured');

} else {

alert('Post ID: ' + response.id);

}

});

}



function fqlQuery(){

FB.api('/me', function(response) {

var query = FB.Data.query('select name, hometown_location, sex, pic_square from user where uid={0}', response.id);

query.wait(function(rows) {



document.getElementById('name').innerHTML =

'Your name: ' + rows[0].name + "<br />" +

'<img src="' + rows[0].pic_square + '" alt="" />' + "<br />";

});

});

}



function setStatus(){

status1 = document.getElementById('status').value;

FB.api(

{

method: 'status.set',

status: status1

},

function(response) {

if (response == 0){

alert('Your facebook status not updated. Give Status Update Permission.');

}

else{

alert('Your facebook status updated');

}

}

);

}
</script>
<div data-role="page">

<div data-role="header">
<h1>Foofys-Facebook Page</h1>
</div><!-- /header -->

<div data-role="content">
<p>you are using foofys facebook app</p>
<div id="fb-root"></div>
<fb:login-button autologoutlink="true" perms="email,user_birthday,status_update,publish_stream"> </fb:login-button>



<p>

<a href="#" onclick="showStream(); return false;">Publish Wall Post</a> |

<!-- <a href="#" onclick="share(); return false;">Share With Your Friends</a> | -->

<a href="#" onclick="graphStreamPublish(); return false;">Publish Stream</a> |

<!-- <a href="#" onclick="fqlQuery(); return false;">FQL Query Example</a> -->

</p>



<textarea id="status" cols="50" rows="5">Write your status here'</textarea>

<br />

<!-- <a href="#" onclick="setStatus(); return false;">Status Set Using Legacy Api Call</a> -->



<br /><br /><br />

<div id="login" style ="display:none"></div>

<div id="name"></div>
</div><!-- /content -->

<div data-role="footer">
<h4>Page Footer</h4>
</div><!-- /footer -->
</div><!-- /page -->

</body>

我无法理解代码中发生的情况,顺便说一句,我刚刚指出了我收到错误的确切位置。

最佳答案

JQM 和 FB api 的技巧是使用图形 API。也就是说,不要使用简单的 javascript FB 包装器,因为它们在暴露于 JQM 的页面处理时不稳定 - 相反,只需使用新的图形/休息 API,检查并避免 FB 核心和您的设置的多次初始化。例如

function updateUserInfo(uid, accessToken) {

var uri = "https://graph.facebook.com/" + uid;
console.log("About to call FP.api with URI " + uri);

$.ajax({
type: "GET",
url: "https://graph.facebook.com/" + uid,
dataType: "json",
success:
(function (response) {
console.log("About to call check profile ...");
$("#p_name").val(response.name);
$("#email").val(response.email);
$("#fb_id").val(response.id);
$.ajax({
type: "POST",
url: "/check_profile",
cache: false,
data: {fb_id: response.id},
success: onCheckSuccess,
error: onError
});
console.log("FB id: " + response.id);
}),
error: onError
});

关于javascript - Facebook 使用 jquery 移动框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7726293/

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