使用PageMethods and Ajax。
确保包含以下使用语句:
using System.Web.Services;
您的 PageMethod 将类似于:
[WebMethod]
public static string fbLogin()
{
/* Do your DB Magic Here */
}
使用 jQuery.ajax 方法:
FB.login(function(response) {
if (response.status === 'connected') {
// Logged into your app and Facebook.
$.ajax({
type: "POST",
url: "YouPageName.aspx/fbLogin",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Do something interesting here.
//Like a redirect
document.location.href = "http://www.somewherecool.com/newPage.aspx";
}
});
} else if (response.status === 'not_authorized') {
// The person is logged into Facebook, but not your app.
} else {
// The person is not logged into Facebook, so we're not sure if
// they are logged into this app or not.
}
});
这只是入门的基本内容。不要忘记充实错误处理等。
但是
当您要进行重定向时,您有点违背了使用 AJAX 的目的。有几种方法可以使用 javascript 来获取要回发的页面。最快、最简单和最简单的方法是使用一个带有普通服务器端点击处理程序的 asp 按钮,该处理程序通过 CSS 隐藏。然后在下面的例子中使用javascipt(jquery)来点击按钮。
按钮
<asp:ImageButton id="ctrlNext" style="display:none" runat="server" ImageUrl="Btn_Next.gif" onclick="ctrlNext_Click"></asp:ImageButton>
javascript
FB.login(function(response) {
if (response.status === 'connected') {
// Logged into your app and Facebook.
//CLick the button
$("#<%=ctrlNext.ClientID%>").click();
} else if (response.status === 'not_authorized') {
// The person is logged into Facebook, but not your app.
} else {
// The person is not logged into Facebook, so we're not sure if
// they are logged into this app or not.
}
});