Feather Member
Posts : 27 Join date : 2011-03-09
| Subject: Facebook Login with AJAX Calls Wed May 04, 2011 9:56 pm | |
| Below I have a facebook login/logout script for my website. The login works fine. The AJAX requests the php file, waits for the "Success" response, and then reloads the page. The logout doesn't. Instead the browser goes to the PHP file with just the word "Success" printed on it. I've separated the xml request as a separate function but that doesn't appear to solve it either. - Code:
-
<script type="text/javascript"> //Ajax XML Loaders var xmlhttp; function loadXMLDoc(url,cfunc) { if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=cfunc; xmlhttp.open("GET",url,true); xmlhttp.send(); } window.fbAsyncInit = function() { FB.init({appId: '215993265079207', 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 } }); }; (function() { var e = document.createElement('script'); e.type = 'text/javascript'; e.src = document.location.protocol + 'https://connect.facebook.net/en_US/all.js'; e.async = true; document.getElementById('fb-root').appendChild(e); }()); function fbloginClick(){ FB.login(function(response) { if (response.session) { if (response.perms) { // user is logged in and granted some permissions. // perms is a comma separated list of granted permissions } else { // user is logged in, but did not grant any permissions } } else { // user is not logged in } }, {perms:'email,user_birthday'} ); } function login(){ //Fire the PHP Facebook Auth Script loadXMLDoc('facebook_auth.php',function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { if (xmlhttp.responseText == "Success"){ window.location = "template.php"; }else{ window.location = "login_failed.php"; } } }); } function fblogoutClick(){ FB.logout(function(response) { }); } function logout(){ //Fire the PHP Logout Script loadXMLDoc('logout.php',function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { if (xmlhttp.responseText == "Success"){ window.location = "template.php"; }else{ window.location = "login_failed.php"; } } }); } </script>
|
|