【发布时间】:2018-05-31 08:39:47
【问题描述】:
我无法使 ajax 上下文工作:-
var msgdata = "";
$.ajax({
type: "POST",
url: "players/" + joinplayer + "/joingame.php",
data: {
userID: "<?php echo $_SESSION['username']; ?>",
gameinfo: JSON.stringify(<?php echo json_encode($_SESSION['profile']['user'], true); ?>)
},
context: msgdata,
success: function(data) {
msgdata = data;
$.ajax({
type: "POST",
url: "renamejoin.php",
data: {
userID: "<?php echo $_SESSION['username']; ?>",
joinID: joinplayer
},
context: msgdata,
success: function(data) {
if (data == "" && msgdata != "") {
// sucessfully joined a player's game
} else {
alert(data);
}
},
error: function() {
alert(joinplayer + "'s game is no longer available.");
}
});
},
error: function() {
alert(joinplayer + "'s game is no longer available.");
}
});
if (msgdata == "");
// start showing games awaiting players again
gamefeedrefresh;
timer = window.setInterval(gamefeedrefresh, 2500);
}
msgdata 在这个最后的 if 语句中始终是 ""。问题似乎是范围问题,但即使是其中一个或两个上下文语句都没有区别。
是否可以解决此范围问题?感谢所有建议。
【问题讨论】:
-
context 必须是一个 OBJECT,而不是一个原语 - 见 documentation
标签: javascript php jquery ajax scope