Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Nathan 11 posts 41 karma points
    Jun 27, 2013 @ 21:33
    Nathan
    0

    AJAX Requests Assistance

    Hey all!

     

    So I have been working on this problem for the better part of two weeks now and nothing that I do seems to bring me any closer to solving the problem. I am trying to implement a system where I can signin to facebook and authenticate the user with one button click. The problem arrises when that button has to submit to facebook to bring authenticated data to the client, and then I need to work with post-back to get that data to the server to authenticate it on my end.

     

    The button works fine. The post-back works fine as well (I can verify that I can MAKE an ajax request via alerts and the like). The problem I'm having is setting up the server to accept an ajax request; and then formatting the client to send it to the proper URL.

     

    I've attempted to run through RESTextentions, make custom ASPX pages, etc. I would appreciate ANY help on this.

  • Nathan 11 posts 41 karma points
    Jun 27, 2013 @ 21:42
    Nathan
    0

    Here is my client-side code:

    window.fbAsyncInit = function () {

        FB.init({

            appId: '324182087714410', // App ID

            channelUrl: '//localhost:12227//channel.html', // Channel File

            status: true, // check login status

            cookie: true, // enable cookies to allow the server to access the session

            xfbml: true  // parse XFBML

        });

        FB.Event.subscribe('auth.login', function (response) {

            alert("logged in boss");

        });

        FB.Event.subscribe('auth.logout', function (response) {

        });

        FB.Event.subscribe('auth.authResponseChange', function (response) {

            // Here we specify what we do with the response anytime this event occurs. 

            if (response.status === 'connected') {

                var uid = response.authResponse.userID;

                var accessToken = response.authResponse.accessToken;

                getData();

                alert("6");

     

            } else if (response.status === 'not_authorized') {

                // the user is logged in to Facebook, 

                // but has not authenticated your app

            } else {

                // the user isn't logged in to Facebook.

            }

        });

    };

    (function (d) {

        var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];

        if (d.getElementById(id)) { return; }

        js = d.createElement('script'); js.id = id; js.async = true;

        js.src = "//connect.facebook.net/en_US/all.js";

        ref.parentNode.insertBefore(js, ref);

    }(document));

     

    function getData() {

        FB.getLoginStatus(function (response) {

            var accessToken = response.authResponse.accessToken;

            $.ajax({

                type: "POST",

                url: "fb_redirect",

                data: '{"field1":"43"}',

                contentType: "application/json; charset=utf-8",

                dataType: "json",

                success: function (msg) {

                    alert("SUCCESS!");

                },

                error: function(data, status, jqXHR) {

                    alert(data);

                }

            });

        });

     

     

    Here is the contents of fb_redirect.master:

    <%@ Master Language="C#" MasterPageFile="~/umbraco/masterpages/default.master" AutoEventWireup="true" %>

     

    <asp:Content ContentPlaceHolderID="ContentPlaceHolderDefault" runat="server">

        <form runat="server">

            <umbraco:Macro Alias="fb_Redirect" runat="server" />

        </form>

    </asp:Content>

    And here is the contents of the user control macro:
    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="loginWithFacebook.ascx.cs" Inherits="usercontrols_loginWithFacebook" %>
    <a href="#" class="btn palette-bkrnd-facebook"><i class="icon-facebook-sign icon-large" style="color: #FFF;"></i>&emsp;Login with Facebook</a>
    <div class="fb-login-button" data-show-faces="true" data-width="400" data-max-rows="1"></div>
    <asp:Button ID="btnLoginFacebook" runat="server" Text="Login with Facebook" OnClick="loginToFacebook" class="fb-login-button" CssClass="btn btn-primary" style="color: #FFF;"/>
    <div class="row-fluid">
        <div class="span2"></div>
        <div class="span10">
            <asp:Label ID="errorConsoleFacebook" runat="server"></asp:Label>
        </div>
    </div>
    *********** code behind **************
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Facebook;
    public partial class usercontrols_loginWithFacebook : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void loginToFacebook(object sender, EventArgs e)
        {
            errorConsoleFacebook.Text = "STASERTAERT";
            String something = Request.Form["loginForm"];
            String s = Request.QueryString["field1"];
            if (String.IsNullOrEmpty(something) && String.IsNullOrEmpty(s))
            {
                errorConsoleFacebook.Text = "true"; Response.Redirect("/");
            }
            else
            {
                errorConsoleFacebook.Text = s;
            }
        }
    }
    ************** What I want to do****************
    I want to have the fb_redirect page return SOMETHING. It can be anything, and then have that displayed as an alert on the client side. Once that happens I can figure out the rest; but it's that initial connection/post-back that I need help on.

     

Please Sign in or register to post replies

Write your reply to:

Draft