【问题标题】:Ajax post wont work, I can't get it rightAjax 帖子无法正常工作,我无法正确处理
【发布时间】:2014-01-14 23:47:23
【问题描述】:

我想不通... ajaxscript 不会运行。在调用脚本之前,一切正常,似乎没有调用该函数。不调用它,它工作正常。

所以我找到了获取我的 Facebook 朋友的代码。脚本很好用,但我想把它写到我的 PHP 数据库中。

这是我添加的代码:

function geefNaam(id, naam){
  $.ajax({
  type: "POST",
  url: "schrijfrecord.php",
  data: { id: id, naam: naam },
  success: window.alert("whatever")
  });
}
</script>

这是完整的脚本

<!DOCTYPE html>

<head>
    <script>
    function geefNaam(id, naam){
          $.ajax({
          type: "POST",
          url: "schrijfrecord2.php",
          data: { id: id, naam: naam },
          success: window.alert("bam!")
          });
        }
    </script>
</head>

<body>
<script src="jquery.js"></script>
    <header>
    </header>
    <center>
        <button id="fb-auth">login</button>
    </center>

    <div id="result_friends"></div>
    <div id="fb-root"></div>

    <script>

    function sortMethod(a, b) {
        var x = a.name.toLowerCase();
        var y = b.name.toLowerCase();
        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    }

    window.fbAsyncInit = function() {
        FB.init({ appId: '<?= $sApplicationId ?>',
            status: true,
            cookie: true,
            xfbml: true,
            oauth: true
        });

        function updateButton(response) {
            var button = document.getElementById('fb-auth');

            if (response.authResponse) { 
                var userInfo = document.getElementById('user-info');
                FB.api('/me', function(response) {
                    userInfo.innerHTML = '<img src="https://graph.facebook.com/' + response.id + '/picture">' + response.name;
                    button.innerHTML = 'Logout';
                });

                FB.api('/me/friends?limit=<?= $iLimit ?>', function(response) {
                    var result_holder = document.getElementById('result_friends');
                    var friend_data = response.data.sort(sortMethod);

                    var results = '';

                    for (var i = 0; i < friend_data.length; i++) {
                        var id = friend_data[i].id;
                        var naam = friend_data[i].name;
                        results += '<div><img src="https://graph.facebook.com/' + id + '/picture">' + naam + '</div>';
                        **geefNaam(id, naam);**
                    }

                    // and display them at our holder element
                    result_holder.innerHTML = '<h2>Result list of your friends:</h2>' + results;

                });

                button.onclick = function() {
                    FB.logout(function(response) {
                        window.location.reload();
                    });
                };
            } else { // otherwise - dispay login button
                button.onclick = function() {
                    FB.login(function(response) {
                        if (response.authResponse) {
                            window.location.reload();
                        }
                    }, {scope:'email'});
                }
            }
        }

        // run once with current status and whenever the status changes
        FB.getLoginStatus(updateButton);
        FB.Event.subscribe('auth.statusChange', updateButton);
    };

    (function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
    }());
    </script>

【问题讨论】:

  • 我希望你可以在你的代码中包含 jquery 脚本,所以我可以在下面的答案中投票

标签: javascript php ajax post


【解决方案1】:

将你的成功方法封装在一个匿名函数中:

function geefNaam(id, naam){
  $.ajax({
  type: "POST",
  url: "schrijfrecord.php",
  data: { id: id, naam: naam },
  success: function() { window.alert("whatever"); }
  });
}

实际上,您只是在此处调用警报,并将其返回值(什么都不是)设置为您的 success 方法。当你用匿名函数包装它时,返回值是一个函数,这是success 正在寻找的。​​p>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    • 2016-05-14
    相关资源
    最近更新 更多