我有以下功能错误,它开始真正的痛苦。
由于某些原因,它是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>
答案 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
后才会被调用。