繁体   English   中英

如何使用POST方法在jquery中发送数据?

[英]How to send data in jquery with POST method?

我需要具有此功能:使用Facebook登录名的用户登录名。 一旦用户连接,就需要使用POST方法发送一些Facebook数据,因此另一个页面(setSes.php)可以处理该数据。

我在下面创建了代码,(添加了一个额外的警报,以查看是否按需加载数据,效果很好),但是以某种方式未调用页面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";

?>

在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');
    }
});

暂无
暂无

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

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