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

时间:2011-07-23 15:52:25

标签: javascript jquery ajax facebook facebook-fql

我使用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完成运行后重定向,这不会发生

事先获得任何帮助.. :)

1 个答案:

答案 0 :(得分:0)

我抓了一针。不确定这是否会完全解决您的问题,但请将其作为起点:

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";
                            });
                        }
                    });
                }
            }
        });
    }
});