Jquery帖子出现痛苦的jquery错误

时间:2012-03-15 15:23:29

标签: javascript jquery

我有以下功能错误,它开始真正的痛苦。

由于某些原因,它是saing postData没有在标有错误的行上定义。但我已经定义了postData并使用get.php中返回的json填充它?这里有什么遗漏。

<script>
 function postToFeed(shoeID) {

    $.post("get.php",{id: shoeID}, 
                function(data){
                               var postData = data;
                               }, "json");  

    var obj = {
      method: 'feed',
      link: 'https://developers.facebook.com/docs/reference/dialogs/',
      picture: 'http://fbrell.com/f8.jpg',
      name: postData.title, //ERROR
      caption: '<?=SHARE_CAPTION?>',
      description: postData.description //ERROR
    };

    function callback(response){
        if(response['post_id']){
            $.post("vote.php",{id: '<?=(isset($_POST['fbid'])? $_POST['fbid'] : $data['id'])?>', shoe: shoeID, type: 'share'}, 
                            function(score){
                            $("#shoe_"+shoeID).html(score)
                            }); 
        }
    }

    FB.ui(obj,callback);
  }
</script>

4 个答案:

答案 0 :(得分:2)

在回调之外移动postData,以便在obj ..

中访问它
var postData;
$.post("get.php",{id: shoeID}, 
           function(data){
               postData = data;
           }, 
           "json");

答案 1 :(得分:2)

由于您已在post的成功处理程序中定义它,因此无法访问它。在外部范围内定义postData它将正常工作。

function postToFeed(shoeID) {
    var postData;//
    $.post("get.php",{id: shoeID}, 
                function(data){
                               postData = data;
                               }, "json");  

....
.... 

这里还有一点需要注意。您正在使用$.post进行ajax异步调用,因此即使您在外部作用域中定义postData它也不会起作用,因为在post请求执行之前postData为null或空。

因此,您应该在$.post的成功处理程序中执行其余代码。试试这个。

function postToFeed(shoeID) {

    $.post("get.php",{id: shoeID}, function(data){
      var postData = data;
      var obj = {
           method: 'feed',
           link: 'https://developers.facebook.com/docs/reference/dialogs/',
           picture: 'http://fbrell.com/f8.jpg',
           name: postData.title, //ERROR
           caption: '<?=SHARE_CAPTION?>',
           description: postData.description //ERROR
      };

      function callback(response){
        if(response['post_id']){
            $.post("vote.php",{id: '<?=(isset($_POST['fbid'])? $_POST['fbid'] : $data['id'])?>', shoe: shoeID, type: 'share'}, 
                            function(score){
                            $("#shoe_"+shoeID).html(score)
                            }); 
        }
      }

      FB.ui(obj,callback);

    }, "json"); //$.post-End 

}

答案 2 :(得分:1)

你不应该在内部函数中定义它,在外面定义它,只在该函数中赋值

<script>
var postData;
function postToFeed(shoeID) {


$.post("get.php",{id: shoeID}, 
            function(data){
                             postData = data;
                           }, "json");  

var obj = {
  method: 'feed',
  link: 'https://developers.facebook.com/docs/reference/dialogs/',
  picture: 'http://fbrell.com/f8.jpg',
  name: postData.title, //ERROR
  caption: '<?=SHARE_CAPTION?>',
  description: postData.description //ERROR
};

function callback(response){
    if(response['post_id']){
        $.post("vote.php",{id: '<?=(isset($_POST['fbid'])? $_POST['fbid'] : $data['id'])?>', shoe: shoeID, type: 'share'}, 
                        function(score){
                        $("#shoe_"+shoeID).html(score)
                        }); 
    }
}

FB.ui(obj,callback);
}
</script>

答案 3 :(得分:0)

您只需稍微重新排列代码:

function postToFeed(shoeID) {
    function callback(response) {
        if (response['post_id']) {
            $.post("vote.php", {
                id: '<?=(isset($_POST['fbid'])? $_POST['fbid'] : $data['id'])?>',
                shoe: shoeID,
                type: 'share'
            }, function(score) {
                $("#shoe_" + shoeID).html(score)
            });
        }
    }

    $.post("get.php", {
        id: shoeID
    }, "json").done(function(postData){
        var obj = {
            method: 'feed',
            link: 'https://developers.facebook.com/docs/reference/dialogs/',
            picture: 'http://fbrell.com/f8.jpg',
            name: postData.title,
            caption: '<?=SHARE_CAPTION?>',
            description: postData.description
        };
        FB.ui(obj, callback);

    });   
}​

注意obj是如何构建的,FB.ui()仅在原始$.post()done后才会被调用。