简体   繁体   English

使用javascript函数调用Ajax方法

[英]Calling Ajax method using javascript function

I'm working with jQuery Mobile. 我正在使用jQuery Mobile。
My Code for move to details div :: 我的代码以转到details div ::

<a href="#details" data-ajax="true" data-transition="pop" data-role="button" data-inline="false" data-icon="info">Go To Details</a>

Now, I want to authenticate user with Facebook. 现在,我想用Facebook验证用户身份。

So I have applied :: 所以我申请了::

<a href="javascript:userLogin()" data-ajax="true" data-transition="pop" data-role="button" data-inline="false" data-icon="info"> Login with Faceook </a>

Function Calling for Authentication 验证功能调用

function userLogin() {
     FB.login(
          function(response) {
               if (response.authResponse) {
                   alert('logged in');

                   FB.api('/me', function(response) {
                     alert("Welcome " + response.name);
                   });
                } else {
                   alert('not logged in');
                }
           },
           { scope: "email" }
     );
}

I'm getting valid user response after authentication. 身份验证后,我得到了有效的用户响应。
Now I want to navigate to that details div . 现在,我想导航到该details div

So, how can I implement that ajax code after successfully login ?? 那么,成功登录后如何实现该Ajax代码?
Any suggestion will be appreciated. 任何建议将不胜感激。

Thanks. 谢谢。

Instead of changing the href attribute directly, you should bind the onclick event to a function returning true . 您应该直接将onclick事件绑定到返回true的函数,而不是直接更改href属性。

Modify your a tag. 修改你的a标签。

<a href="#details" onclick="javascript:return userLogin()" data-ajax="true" data-transition="pop" data-role="button" data-inline="false" data-icon="info">Go To Details</a>

Modify your userLogin function: 修改您的userLogin函数:

function userLogin() {
    FB.login(
        function(response) {
            if (response.authResponse) {
                alert('logged in');

                FB.api('/me', function(response) {
                    alert("Welcome " + response.name);
                });
            } else {
                alert('not logged in');
            }
        },
        { scope: "email" }
    );
    return true;
}

It seems that FB.login is an asynchronized operation. 似乎FB.login是一个异步操作。 If you want to redirect after the login action, try to modify the location.hash after login. 如果要在登录操作后重定向,请尝试在登录后修改location.hash Note that the return value has changed to false . 请注意,返回值已更改为false

function userLogin() {
    var hash = '#details';
    FB.login(
        function(response) {
            if (response.authResponse) {
                alert('logged in');
                FB.api('/me', function(response) {
                    alert("Welcome " + response.name);
                    window.location.hash = hash;
                });
            } else {
                alert('not logged in');
            }
        },
        { scope: "email" }
    );
    return false;
}

Use $.ajax() for making a call to the Facebook API to authenticate and in the callback function of the ajax function, call the click event of the details link. 使用$ .ajax()调用Facebook API进行身份验证,然后在ajax函数的回调函数中,调用详细信息链接的click事件。

Maybe something like this, 也许像这样

  $.ajax({
     url : 'https://graph.facebook.com/me'+whatever,
     success : function(){
        $('#details').click()
     }
  });

The success function will be called when the ajax call is successful. 当ajax调用成功时,将调用成功函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM