Php 为什么赢了';在facebook登录期间是否运行此ajax?
我正在我的网站上运行一个特殊的facebook登录,当用户点击它时,必须出现以下情况:Php 为什么赢了';在facebook登录期间是否运行此ajax?,php,ajax,facebook,facebook-login,Php,Ajax,Facebook,Facebook Login,我正在我的网站上运行一个特殊的facebook登录,当用户点击它时,必须出现以下情况: 连接FB 如果用户数据库中已识别facebook ID,请执行XXX 如果无法识别facebook ID,请执行YYY 由于某些原因,我无法获得检查facebook ID是否正常工作的ajax查询。我已经花了好几个小时在这上面,但仍然没有运气(开始感觉像一个实时墨水)。。。即使我将查询设置为仅返回0(例如,将第二个echo也更改为“echo 0”),它也不会返回(除非我将整个函数拆分为“echo 0”)。
- 连接FB
- 如果用户数据库中已识别facebook ID,请执行XXX
- 如果无法识别facebook ID,请执行YYY
谢谢我用你的代码实现了它,它运行得很好。
以下是PHP代码的所有内容:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
function fblogin() {
FB.login(function(response) {
if (response.authResponse) {
var fbid = response.authResponse.userID;
$.post("http://johnhiott.com/play/fbid.php", {'fbid': fbid}, function(result){
if(result == 0){
alert("in");
return true;
}else{
alert("no");
return false;
}
});
}else {
return false;
}
}, {scope: 'email'});
}
</script>
</head>
<body>
<div id="fb-root"></div>
<script>
// Additional JS functions here
window.fbAsyncInit = function() {
FB.init({
appId : '284563394889810', // App ID
channelUrl : '//WWW.JOHNHIOTT.COM/play/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// Additional init code here
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>
</body>
<a href="#" onClick="fblogin()">hi</a>
</script>
</html>
函数fblogin(){
FB.登录(功能(响应){
if(response.authResponse){
var fbid=response.authResponse.userID;
$.post(”http://johnhiott.com/play/fbid.php“,{'fbid':fbid},函数(结果){
如果(结果==0){
警报(“in”);
返回true;
}否则{
警告(“否”);
返回false;
}
});
}否则{
返回false;
}
},{范围:'电子邮件'});
}
//这里有额外的JS函数
window.fbAsyninit=函数(){
FB.init({
appId:'284563394889810',//应用ID
channelUrl:'//WWW.JOHNHIOTT.COM/play/channel.html',//频道文件
状态:true,//检查登录状态
cookie:true,//启用cookie以允许服务器访问会话
xfbml:true//解析xfbml
});
//这里有额外的初始化代码
};
//异步加载SDK
(职能(d){
var js,id='facebook jssdk',ref=d.getElementsByTagName('script')[0];
if(d.getElementById(id)){return;}
js=d.createElement('script');js.id=id;js.async=true;
js.src=“//connect.facebook.net/en_US/all.js”;
ref.parentNode.insertBefore(js,ref);
}(文件);
您的jquery代码看起来应该可以工作,如果它在您的PHP只是一个echo 0语句时可以工作,那么您可能在PHP中返回的不仅仅是1或0。您是否查看了PHP脚本的具体返回内容?不幸的是,我不知道如何直接输出ajax的输出(似乎总是需要打印一些内容并获得结果)。但是,我知道您所描述的并不是问题所在,因为我已经尝试从上面获取准确的php代码,并将其设置为else echo 0(因此在这两种情况下它都应该echo 0),并且函数仍然会像没有返回0一样执行。这对我来说很奇怪,我想知道这是否与facebook元素有关。如果不知道你的PHP文件真正返回了什么,我就不能肯定。我刚刚用FacebookJSSDK实现了你的代码,效果很好。我想知道您的PHP是否显示警告或错误。尝试注释DB代码并返回0或1。有点奇怪,它碰巧开始工作了,尽管我从来没有弄清楚问题是什么。。。也许事情已经被缓存了?(因为页面出现了可见的更新,所以这没有什么意义…@John:你能告诉我jquery调用发生在哪一行吗?我刚试着用facebool按钮运行你的代码
// FB login function
function fblogin() {
FB.login(function(response) {
if (response.authResponse) {
// Here, I want to test if the facebook ID is already in the table
var fbid = response.authResponse.userID;
$.post("check_fbid.php", {'fbid': fbid}, function(result)
{
if(result == 0)
{
// login in the user
// use fbid to call session, etc. for login
location.reload();
return true;
}
else
{
// open new box on page to let user register
openRegisterBox();
return false;
}
});
} else {
// Login was cancelled or failed
return false;
}
}, {scope: 'email'});
}
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
function fblogin() {
FB.login(function(response) {
if (response.authResponse) {
var fbid = response.authResponse.userID;
$.post("http://johnhiott.com/play/fbid.php", {'fbid': fbid}, function(result){
if(result == 0){
alert("in");
return true;
}else{
alert("no");
return false;
}
});
}else {
return false;
}
}, {scope: 'email'});
}
</script>
</head>
<body>
<div id="fb-root"></div>
<script>
// Additional JS functions here
window.fbAsyncInit = function() {
FB.init({
appId : '284563394889810', // App ID
channelUrl : '//WWW.JOHNHIOTT.COM/play/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// Additional init code here
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>
</body>
<a href="#" onClick="fblogin()">hi</a>
</script>
</html>