如何使用ajax将javascript对象属性转换为php变量?

时间:2018-06-17 11:35:15

标签: javascript php jquery ajax

我有一个javascript对象,其中包含一些值。我正在使用jquery-ajax将其发送到php。 在php文件中如何获取javascript对象属性并将其存储到php变量

这是我的代码(javascript对象和ajax调用)

    var script = document.createElement("SCRIPT");
    script.src = "http://code.jquery.com/jquery-3.2.1.min.js"
    integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
    crossorigin="anonymous";
    script.type = 'text/javascript';
    document.getElementsByTagName("head")[0].appendChild(script);


   var person = { userID: "", name: "", accessToken: "", picture: "", email: ""};

        function logIn() {
            FB.login(function (response) {
                if (response.status == "connected") {
                    person.userID = response.authResponse.userID;
                    person.accessToken = response.authResponse.accessToken;

                    FB.api('/me?fields=id,name,email,picture.type(large)', function (userData) {
                        person.name = userData.name;
                        person.email = userData.email;
                        person.picture = userData.picture.data.url;
                        // ajax code start
                        $.ajax({
                           url: "insert.php",
                           method: "POST",
                           data: person,
                           dataType: 'text',
                           success: function (serverResponse) {

                               if (true)
                                   window.location = "index.php";
                           }
                        });
                        // ajax code end
                    });
                }
            }, {scope: 'public_profile, email'})
        }

        function logout(){
           FB.getLoginStatus( function(response){
              if(response.status == 'connected'){
                  FB.logout( function(response){
                      $.ajax({
                          url : 'logout.php',
                          success : function(data){
                              if(true){
                                  window.location = 'login.php';
                              }
                          }
                      });
                  });
              };
           });
        }

        window.fbAsyncInit = function() {
            FB.init({
                appId            : '',
                autoLogAppEvents : true,
                xfbml            : true,
                version          : 'v2.11'
            });
        };

        (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 = "https://connect.facebook.net/en_US/sdk.js";
            fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));

按钮点击后人物对象存储的地方,并且该数据将来自Facebook API呼叫..

在insert.php中(我觉得这样)

<?php 

  require_once 'dbconfig.php';

    if($_POST){
        $userID = $_POST['userID'];
        $name = $_POST['name'];
        $email = $_POST['email'];
        $picture = $_POST['name'];
        $access_token = $_POST['access_token'];
    }



    $stmt = $db_con->prepare('INSERT INTO users(userID,uname,email,picture,access_token) VALUES(:uid, :uname, :email ,:picture, :access_token)');
    $stmt->bindParam(':uid',$userID);
    $stmt->bindParam(':uname',$name);
    $stmt->bindParam(':email',$email);
    $stmt->bindParam(':picture',$picture);
    $stmt->bindParam(':access_token',$access_token);
    $stmt->execute();    
   }
?>

执行操作后,它将成功重定向到index.php,但值不存储在数据库中。 可能是什么原因.. 提前谢谢

0 个答案:

没有答案