2017-10-17 140 views
0

我需要這個功能:使用Facebook登錄的用戶登錄。只要用戶連接,一些Facebook數據需要使用POST方法發送,所以另一個頁面(setSes.php)可以處理數據。如何使用POST方法在jQuery中發送數據?

我已經創建了下面的代碼,(添加了一個額外的警報,看看數據是否應該加載,它工作得很好),但不知何故頁面setSes.php沒有被調用。

function login() { 
     FB.login(function(response) { 
     if (response.status === 'connected') { 
      document.getElementById('status').innerHTML = 'Connected'; 

      FB.api('/me', 'GET', {fields: 'first_name,last_name,name,id,link,gender,locale,picture.width(9999).height(9999)'}, function(response) { 
       var id=response.id; 
       var firstName=response.first_name; 
       var lastName=response.last_name; 
       var picture=response.picture.data.url; 
       alert("Selected ID= "+id); 

       $.ajax({ 
       url:"/setSes.php ", 
       method:"POST", 

       data:{ 
        UID: "id", 
        firstName: "firstName", 
        lastName: "lastName", 
        picture: "picture" 
       } 
       }); 
      }); 
     } else if (response.status === 'not_authorized') { 
      document.getElementById('status').innerHTML = 'Not connected'; 
     } else { 
      document.getElementById('status').innerHTML = 'Not able to login'; 
     } 
     }, {scope: 'email'}); 
    } 

setSes.php

<? 
include_once 'includes/db_connect.php'; 
include_once 'includes/functions.php'; 

sec_session_start(); 

$stmt = $mysqli->prepare("INSERT INTO users(first_name, last_name, oauth_uid, picture 
    ) VALUES (?, ?, ?, ?)"); 
    $stmt->bind_param('ssss', $_POST["firstName"], $_POST["lastName"], $_POST["UID"], $_POST["picture"]); 
    $stmt->execute(); 
    $stmt->close(); 

    echo "I ran"; 

?> 
+1

'型=「POST」'會做的伎倆。刪除你的'方法' –

+0

nope,不幸沒有工作:( –

+0

然後去與$ .post https://api.jquery.com/jquery.post/ –

回答

1

在Ajax調用你傳遞字符串,而不是獲得的變量。您的意見時,setSes.php文件在同一目錄中,所以試試這個...

var id = response.id, 
    fName = response.first_name, 
    lName = response.last_name, 
    pic = response.picture.data.url; 

$.ajax({ 
    method: "POST", 
    url: "setSes.php", 
    data: { UID: id, firstName: fName, lastName: lName, picture: pic }, 
    success: function(response) { 
     alert('SUCCESS: ' + response); 
    }, 
    error: function() { 
     alert('ERROR'); 
    } 
}); 
+0

沒有訣竅:( –

+0

是'setSes.php'與jQuery代碼文件位於同一目錄嗎? –

+0

是的,它在同一個目錄。 –

相關問題