从JavaScript .NET MVC调用Controller Action

时间:2017-03-25 23:58:01

标签: javascript asp.net ajax asp.net-mvc

用户可以通过Facebook登录我的应用程序。在用户按下按钮以启动该功能并通过Facebook成功进行身份验证后,我希望启动登录的页面以及从Facebook收到成功登录信息的页面基本上重定向到另一个视图。然后,用户可以完成我的应用程序的注册过程。我花时间阅读其他解决方案,我知道我可能必须使用.AJAX。哪个好。问题是它似乎没有起作用。

<script>
  // Called with the results from FB.getLoginStatus().
  function statusChangeCallback(response) {
  console.log('statusChangeCallback');
  console.log(response);

// The response object returns with a status field that lets the
// app know the current login status of the person.

    if (response.status === 'connected') {
  // User is logged into your app and Facebook.
    console.log("You are connected.");
    completeLogin();
    console.log("completeLogin called")
  testAPI();
} else {
  // The person is not logged into your app or we are unable to tell.
  document.getElementById('status').innerHTML = 'Please log ' +
    'into this app.';
}
  }

  // This function is called when someone finishes with the Login
  // Button.  See the onlogin handler attached to it in the sample
  // code below.
  function checkLoginState() {
    FB.getLoginStatus(function(response) {
      statusChangeCallback(response);
    });
  }

  function completeLogin() {
      $.ajax({
              type: "GET",
              url: '@Url.Action("List", "PotentialClient")'
      }).done(function () {
          console.log('Called ExternalLoginConfirmation');
      });
   }

  window.fbAsyncInit = function() {
  FB.init({
      appId: '1614609378567893',
      cookie     : true,  // enable cookies to allow the server to access
                    // the session
      xfbml      : true,  // parse social plugins on this page
      version    : 'v2.8' // use graph api version 2.8
    });

  // Now that we've initialized the JavaScript SDK, we call
   // FB.getLoginStatus() gets the state of the
  // person visiting this page and can return one of three states to
  // the callback provided:
  //
  // 1. Logged into your app ('connected')
  // 2. Logged into Facebook, but not your app ('not_authorized')
  // 3. Not logged into Facebook and can't tell if they are logged into
   //    your app or not.
  //
  // handles these three cases in the callback function.

  FB.getLoginStatus(function(response) {
    statusChangeCallback(response);
  });

  };

  // Load the SDK asynchronously
  (function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));

  // Running a simple test of the Graph API after login is
  // successful.  
  function testAPI() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
      console.log('Successful login for: ' + response.name);
      document.getElementById('status').innerHTML =
        'Thanks for logging in, ' + response.name + '!';
    });
  }
    </script> 

在我的Ajax函数中,&#34; List&#34;,是我特定的控制器动作,&#34; PotentialClient&#34;控制器类。

运行此代码后,按下&#34;通过Facebook登录&#34;按钮,在开发人员控制台中,我没有看到有关网址路径的任何错误。

但页面没有重定向到我需要的视图。

我最有可能忽略或不做什么?

谢谢你, CM

0 个答案:

没有答案