在 ajax 完成之前页面被重定向

发布于 2024-11-26 01:26:14 字数 3629 浏览 0 评论 0原文

我使用 ajax 创建一个会话,并在用户单击这样的按钮时重定向页面。我在 facebook api 中使用它(使用 api 使用 user.id 创建会话),

    FB.login(function(response) {
            if (response.session) {
                FB.api('/me', function(user) {
                if (user!=null) {
                    var request = new XMLHttpRequest();         
                    if(document.getElementById("ans2").value==""){
                        document.getElementById("belowbutton2").innerHTML ="Don't leave it blank!!";
                    }
                    else{
                        var request2 = new XMLHttpRequest();

                        request.onreadystatechange=function(){

                            if(request.readyState==4 && request.status==200){
                                    document.getElementById("debugger").innerHTML = request.responseText;
                                    window.location = "weekques/weekques.php";
                                }
                        }
                        var uid = user.id;   
                        alert(uid);     
                        var jqXHR = ($.ajax)({url:"sessions.php?uid="+uid,
                                            async:false,
                                            cache: false,
                                            timeout: 30000,
                                            error:function(){
                                                window.location = "http://www.xyz.com";
                                            },
                                            success:function(){
                                                    request.open("GET", "weekques/answer.php?ans="+document.getElementById("ans2").value, true); //+"&qid="+qidjs
                                                    request.send();
                                                }
                                            });



                        }
                     }
                 });
            } 
        });

但问题是窗口在创建会话之前正在重定向.. 这是

sessions.php文件,

    <?php

    session_start();


    require_once("connection.php");
    $user=mysql_query("SELECT * from `thebirbals`.`FBusers` where uid='$uid';");


    $row_count=mysql_num_rows($result);


        $_SESSION['uid']=$_GET["uid"];
        $uid = $_SESSION['uid'] ;


    if($row_count==1){

        $_SESSION['name'] = $check["name"];
        $_SESSION['profile_link'] = $check["profile_link"];
        $_SESSION['dp'] = $check["dp"];
    }

    else{


        require_once('facebook/src/facebook.php');

        $facebook = new Facebook(array(
         'appId'  => '1550598824560526',
         'secret' => '4cf28242b5abfa26be8fd3e2074e5724',
         'cookie' => false
         ));


         $fql = "SELECT first_name,profile_url,pic_small from user where uid=$uid";

         $response = $facebook->api(array(
         'method' => 'fql.query',
         'query' =>$fql,
         ));


         foreach($response as $val)
         {

            $_SESSION['name']=$val["first_name"];
            $_SESSION['dp']=$val["pic_small"];
            $_SESSION['profile_link']= $val["profile_url"];

            $name = $val["first_name"];
            $profile_link = $val["profile_url"];
            $dp = $val["pic_small"];
            echo "done";

        }

        $insert=mysql_query("INSERT INTO  `thebirbals`.`FBusers` ( `uid`, `name`, `profile_link`, `dp`) VALUES ('$uid', '$name', '$profile_link', '$dp');");


    }   
?>

我想在sessions.php运行完成后重定向的

这不会提前发生以获得任何帮助..:)

i use ajax to create a session and to redirect the page when the user clicks on a button like this .. im using this in the facebook api(using the api to create a session with the user.id)

    FB.login(function(response) {
            if (response.session) {
                FB.api('/me', function(user) {
                if (user!=null) {
                    var request = new XMLHttpRequest();         
                    if(document.getElementById("ans2").value==""){
                        document.getElementById("belowbutton2").innerHTML ="Don't leave it blank!!";
                    }
                    else{
                        var request2 = new XMLHttpRequest();

                        request.onreadystatechange=function(){

                            if(request.readyState==4 && request.status==200){
                                    document.getElementById("debugger").innerHTML = request.responseText;
                                    window.location = "weekques/weekques.php";
                                }
                        }
                        var uid = user.id;   
                        alert(uid);     
                        var jqXHR = ($.ajax)({url:"sessions.php?uid="+uid,
                                            async:false,
                                            cache: false,
                                            timeout: 30000,
                                            error:function(){
                                                window.location = "http://www.xyz.com";
                                            },
                                            success:function(){
                                                    request.open("GET", "weekques/answer.php?ans="+document.getElementById("ans2").value, true); //+"&qid="+qidjs
                                                    request.send();
                                                }
                                            });



                        }
                     }
                 });
            } 
        });

but the problem is that the window is redirecting before the session is created ..
heres the

sessions.php file

    <?php

    session_start();


    require_once("connection.php");
    $user=mysql_query("SELECT * from `thebirbals`.`FBusers` where uid='$uid';");


    $row_count=mysql_num_rows($result);


        $_SESSION['uid']=$_GET["uid"];
        $uid = $_SESSION['uid'] ;


    if($row_count==1){

        $_SESSION['name'] = $check["name"];
        $_SESSION['profile_link'] = $check["profile_link"];
        $_SESSION['dp'] = $check["dp"];
    }

    else{


        require_once('facebook/src/facebook.php');

        $facebook = new Facebook(array(
         'appId'  => '1550598824560526',
         'secret' => '4cf28242b5abfa26be8fd3e2074e5724',
         'cookie' => false
         ));


         $fql = "SELECT first_name,profile_url,pic_small from user where uid=$uid";

         $response = $facebook->api(array(
         'method' => 'fql.query',
         'query' =>$fql,
         ));


         foreach($response as $val)
         {

            $_SESSION['name']=$val["first_name"];
            $_SESSION['dp']=$val["pic_small"];
            $_SESSION['profile_link']= $val["profile_url"];

            $name = $val["first_name"];
            $profile_link = $val["profile_url"];
            $dp = $val["pic_small"];
            echo "done";

        }

        $insert=mysql_query("INSERT INTO  `thebirbals`.`FBusers` ( `uid`, `name`, `profile_link`, `dp`) VALUES ('$uid', '$name', '$profile_link', '$dp');");


    }   
?>

i want to redirect after the sessions.php is finished running this does not happen

ty in advance for any help .. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

且行且努力 2024-12-03 01:26:14

我刺了一下。不确定这是否能完全解决您的问题,但请以此为起点:

FB.login(function (response) {
    if (response.session) {
        FB.api('/me', function (user) {
            if (user != null) {
                if (document.getElementById("ans2").value == "") {
                    document.getElementById("belowbutton2").innerHTML = "Don't leave it blank!!";
                }
                else {
                    var uid = user.id;
                    alert(uid);
                    $.ajax({ url: "sessions.php?uid=" + uid,
                        async: false,
                        cache: false,
                        timeout: 30000,
                        error: function () {
                            window.location = "http://www.xyz.com";
                        },
                        success: function () {
                            $.get("weekques/answer.php", $.param({ "ans": document.getElementById("ans2").value }), function (data) {
                                alert("Answer received");
                                document.getElementById("debugger").innerHTML = data;
                                window.location = "weekques/weekques.php";
                            });
                        }
                    });
                }
            }
        });
    }
});

I took a stab. Not sure if this will fix your issue entirely, but take it as a starting point:

FB.login(function (response) {
    if (response.session) {
        FB.api('/me', function (user) {
            if (user != null) {
                if (document.getElementById("ans2").value == "") {
                    document.getElementById("belowbutton2").innerHTML = "Don't leave it blank!!";
                }
                else {
                    var uid = user.id;
                    alert(uid);
                    $.ajax({ url: "sessions.php?uid=" + uid,
                        async: false,
                        cache: false,
                        timeout: 30000,
                        error: function () {
                            window.location = "http://www.xyz.com";
                        },
                        success: function () {
                            $.get("weekques/answer.php", $.param({ "ans": document.getElementById("ans2").value }), function (data) {
                                alert("Answer received");
                                document.getElementById("debugger").innerHTML = data;
                                window.location = "weekques/weekques.php";
                            });
                        }
                    });
                }
            }
        });
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文