我有一个用于登录目的的表单:
<form id = "membershipInfo" method = "post" action = "Login.aspx">
使用提交按钮,我想做一个post方法和一个像这样的javascript onclick方法:
<input type ="submit" id = "submitInfo" class = "MemberInfo" value = "Take Me There!" onclick = "authorize(accept, reject)"/>
onclick中的方法是一种facebook授权方法,它将从用户(访问令牌)中提取信息。我需要此令牌让用户继续我的程序。问题是当单击此按钮时,post方法将在onclick完成之前发生,这意味着访问令牌将永远不会与表单一起传递。
要获取页面加载方法的访问令牌,我使用以下形式的隐藏输入:
<input type = "hidden" id = "hiddenAccessToken" name = "accessToken" value = "<%=accessToken %>" />
在Facebook方法中,我得到了这样的访问令牌:
function authorize(successCallback, failureCallback) {
FB.getLoginStatus(function (response) {
if (response.session) {
// logged in and connected user, carry on
session = response.session;
//Set access token
accessToken = response.session.access_token;
//Call success callback
successCallback();
} else {
// no user session available, Lets ask for perms
FB.ui(
{
method: 'permissions.request',
perms: permissionString
},
function (response) {
if (response && response.session != null) {
//Get session
session = response.session;
//Set access token
accessToken = response.session.access_token;
//Call success callback
successCallback();
} else {
//Call failure callback
failureCallback();
}
});
}
});
//Method hit on successCallback
function accept() {
//Add access token to hidden input
$('#hiddenAccessToken').val(accessToken);
}
我不想使用超时(我不认为它会继续工作),但我需要减慢页面加载速度,直到这些js方法完成。有什么建议吗?
答案 0 :(得分:1)
为什么不用js提交表单,如下所示:
<input type ="button" id = "submitInfo" class = "MemberInfo" value = "Take Me There!" onclick = "authorize(accept, reject)"/>
然后:
function accept() {
//Add access token to hidden input
$('#hiddenAccessToken').val(accessToken);
$('#membershipInfo').submit();
}