Ajax帖子不起作用,我无法做到

时间:2014-01-14 23:47:24

标签: javascript php ajax post

我无法弄明白...... 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>

1 个答案:

答案 0 :(得分:2)

将您的成功方法包装在匿名函数中:

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

实际上,您只是在那里调用警报,并将其返回值(这不是任何内容)设置为success方法。当您使用匿名函数包装它时,返回值是一个函数,这是success正在寻找的函数。