我在谷歌上搜索了关于SO和文档的各种问题。 我发现在按钮/链接点击或表单提交事件上发生了jquery帖子。
我正在使用facebook JS sdk。我在我的页面上保留了facebook登录按钮。如果在网站上第一次使用,当用户登录用户喜欢的电影将存储在数据库中然后使用ajax重定向到新页面时,将显示按钮。
以前我编码用户单击按钮时执行的操作。
现在我希望它自动检查登录状态(发生这种情况),将数据发布到x.php(此页面将插入到db中),然后重定向到新页面y.php。
我无法弄清楚如何在没有$( "a" ).click(function( event )
或submit
的情况下执行此操作。
如果有人可以提供帮助,我在这里编码:
<!DOCTYPE html>
<html xmlns:fb="https://www.facebook.com/2008/fbml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<title>ThenWat</title>
</head>
<body>
<div id="fb-root"></div>
<script type="text/javascript">
var button;
var userInfo;
window.fbAsyncInit = function() {
FB.init({ appId: '178812232292862',
status: true,
cookie: true,
xfbml: true,
oauth: true});
showLoader(true);
function updateButton(response) {
button = document.getElementById('fb-auth');
userInfo = document.getElementById('user-info');
if (response.authResponse) {
//user is already logged in and connected
FB.api('/me?fields=id,name,movies,email', function(info) {
console.log(info.movies);
login(response, info);
});
button.onclick = function() {
FB.logout(function(response) {
logout(response);
});
};
} else {
//user is not connected to your app or logged out
button.innerHTML = 'Login';
button.onclick = function() {
showLoader(true);
FB.login(function(response) {
if (response.authResponse) {
FB.api('/me', function(info) {
login(response, info);
});
} else {
//user cancelled login or did not grant authorization
showLoader(false);
}
}, {scope:'email,user_birthday,status_update,user_about_me'});
}
}
}
// run once with current status and whenever the status changes
FB.getLoginStatus(updateButton);
FB.Event.subscribe('auth.statusChange', updateButton);
};
(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 login(response, info){
**var json = JSON.stringify(info.movies.data);
var a = JSON.parse(json);
console.log(a);
alert(a);** // a I want to post to some new page 'movies_db.php' and then redirect to new page which I could not do**
if (response.authResponse) {
var accessToken = response.authResponse.accessToken;
userInfo.innerHTML = info.name
button.innerHTML = 'Logout';
showLoader(false);
document.getElementById('other').style.display = "block";
}
}
function logout(response){
userInfo.innerHTML = "";
document.getElementById('debug').innerHTML = "";
document.getElementById('other').style.display = "none";
showLoader(false);
}
function showLoader(status){
if (status)
document.getElementById('loader').style.display = 'block';
else
document.getElementById('loader').style.display = 'none';
}
</script>
<button id="fb-auth">Login</button>
<div id="loader" style="display:none">
<img src="ajax-loader.gif" alt="loading" />
</div>
<br />
<div id="user-info"></div>
<br />
<div id="debug"></div>
</body>
</html>