dqrm8199 2013-08-07 11:48
浏览 22
已采纳

在javascript函数中检索变量值

I am using facebook API for my project and getting some data in following manner in var mydata

if (response.status === "connected")
        {
            LodingAnimate(); //Animate login
            FB.api('/me?fields=movies,email', function(mydata) { //--
            console.log(mydata);
              if(data.email == null)
              {
                 alert("You must allow us to access your email id!");
                 ResetAnimate();
             }
   }

I dont have any issue with this code. But I want to send this data using ajax call to process and insert into database.

My ajax call:

function AjaxResponse()
 {
    var send=document.ge(mydata)      **//Here I want to fetch mydata from previous code**
    var datas = document.elements['id'].value;
    var s = 'connect=1'; 
     $.ajax({
    type: "POST",
    url: "process_facebook.php",
    data: s,send                   **//Is this correct way to send s and send togather? I have tried with only 's' which works fine but dont know about both togather**
    }).done(function(result) {
    $("#fb-root").html(result);
    });
   }

Can someone please assist how to fetch mydata in javascript function and review the code.

  • 写回答

1条回答 默认 最新

  • dot_0620 2013-08-07 11:51
    关注

    Since the Facebook API call you're making is asynchronous, you can't return the value from your ge function.

    Instead, use a callback, just like Facebook and everyone else does. See below.

    Separately, the answer to the second unrelated question hidden in your code snippet is "no, that's not how you do that". I've given you a pointer to how to do that below as well.

    function AjaxResponse()
    {
        // Callback here----v arg ---v
        document.ge(mydata, function(send) {
            var datas = document.elements['id'].value;
            $.ajax({
                type: "POST",
                url: "process_facebook.php",
                data: {
                   connect: 1,
                   paramname: mydata      // <=== I don't know what the name of this param is
                }
            }).done(function(result) {
            $("#fb-root").html(result);
        });
    }
    

    ..and have your code call the callback when the Facebook call calls its callback.

    function ge(data, callback) {
        // ...
        if (response.status === "connected") {
            LodingAnimate(); //Animate login
            FB.api('/me?fields=movies,email', function (mydata) { //--
                console.log(mydata);
                if (data.email == null) {
                    alert("You must allow us to access your email id!");
                    ResetAnimate();
                }
                else {
                    callback(data); // <=== Trigger the callback
                }
            }
           // ...
        }
        // ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来