在ajax请求中发布变量

时间:2013-10-16 11:23:09

标签: php ajax

我正在获取用户影片并使用ajax requst将其发布到另一个页面,如下所示。我想发送一些其他变量以及现有的var a

FB.api('/me?fields=movies,email', function(mydata) {
    console.log(mydata.email);
    console.log(mydata.id); 
    var myid=mydata.id;
    var name=mydata.name;
    var email=mydata.email; 
    var imgpath="https://graph.facebook.com/"+myid+"/picture?type=small";
    // I want to send myid,name,email with a in below code. How can I do and receive it on movies_db.php?

    $.post('movies_db.php',{'myd':a},function(data){
        $.ajax({
            url:'url.php'
                ,async:     true
                ,cache:     false
                ,dataType:  'html'
                ,success:   function(data){
                $('body').html(data);
                FB.XFBML.parse();
            }
        });
    });

我想使用以下代码发送myid,名称,电子邮件。我怎样才能在movies_db.php上接收它?

3 个答案:

答案 0 :(得分:1)

您可以根据需要传递任意数量的键/值对

$.post('movies_db.php',{'myd':a, name: name, email: email, parameter_name: value}, ...

在你的PHP中 - movies_db.php

$email = $_POST['email'];
$name = $_POST['name'];
$other = $_POST['parameter_name'];

答案 1 :(得分:0)

这个怎么样?

FB.api('/me?fields=movies,email', function(mydata) {
    console.log(mydata.email);
    console.log(mydata.id); 
    var myid=mydata.id;
    var name=mydata.name;
    var email=mydata.email; 
    var imgpath="https://graph.facebook.com/"+myid+"/picture?type=small";
    // I want to send myid,name,email with a in below code. How can I do and receive it on movies_db.php?

    $.post('movies_db.php',{'myd':myid, 'name': name, 'email': email},function(data){
        $.ajax({
            url:'url.php'
                ,async:     true
                ,cache:     false
                ,dataType:  'html'
                ,success:   function(data){
                $('body').html(data);
                FB.XFBML.parse();
            }
        });
    });
}

答案 2 :(得分:0)

从数据创建对象并将其发布到服务器。

 var myid=mydata.id;
 var name=mydata.name;
 var email=mydata.email; 
 var imgpath="https://graph.facebook.com/"+myid+"/picture?type=small";
 var inputdata = {}
 inputdata = {'myid': myid,
         'name' : name,
         'email' : email,
         'imgpath' : imgpath
        }


 // Use inputdata as post data here
 $.post('movies_db.php',inputdata,function(data){
        $.ajax({
            url:'url.php'
                ,async:     true
                ,cache:     false
                ,dataType:  'html'
                ,success:   function(data){
                $('body').html(data);
                FB.XFBML.parse();
            }
        });
    });

现在您可以访问php文件中的POST数据。