我在谷歌上浏览了关于 SO 和文档的各种问题。我发现 jquery post 发生在按钮/链接单击或表单提交事件上。
我正在使用 facebook JS sdk。我在我的页面上保留了 facebook 登录按钮。如果是第一次在现场使用,用户登录时会出现按钮,用户喜欢的电影将存储在数据库中,然后使用 ajax 重定向到新页面。
以前我编写了用户单击按钮时执行操作的位置。
现在我希望它自动检查登录状态(发生这种情况),将数据发布到 x.php(此页面将插入到数据库中),然后重定向到新页面 y.php。
$( "a" ).click(function( event )
我不知道没有or怎么办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>