javascript post 不执行功能但加载 url

javascript post not doing function but load url

我正在创建 Stratego。
(错误更往下)
所以我在页面加载时创建地图:

turn = true;
komboType = "2kombo";
GameOpsetning = [];
GameOpsetningEnemy = [];
$(document).ready(function() {   
    createmap();
}

然后我做一些控制台日志以确保它有效..

function createmap () {
if (!GameStarted) {
    console.log("GameOpsetning" + GameOpsetning);
    if(GetSetup(Gameid)){
        console.log("GameOpsetning1" + GameOpsetning);
    }
    else
    {
        console.log("GetSetup : error");
    }

这里出现了错误,它将打开文件 "game.php" 而没有错误。但它不会在 post/funcion 中做任何事情。它不会发出警报或 if 或 console.log?

    function GetSetup (Gameid) {
    console.log("GameOpsetning3: " + GameOpsetning);
    if ($.post("game.php", { gameid: Gameid }, function(data) {
        alert(data);
        var data2 = JSON.stringify(data);
        alert(data2);
        //var json = $.parseJSON(data);
        if (data2.status2 && data2.status2 != "false") {
            console.log("data.game: " + JSON.stringify(data.game));
            GameOpsetning = JSON.stringify(data.game);
            GameOpsetningEnemy = JSON.stringify(data.enemygame);
            komboType = data.type;
            turn = data.turn;
            console.log("GameOpsetning5: " + GameOpsetning);
            return true;
        }
        else
        {
            console.log("error: ");
            return false;
        }
    })){
        console.log("post: done");
    }else{
        console.log("post: error");
    }
    console.log("GameOpsetning4: " + GameOpsetning);

}

控制台输出:

GameOpsetning
GameOpsetning3: 
post: done
GameOpsetning4: 
GetSetup : error

没有控制台错误,页面是 return JSON

它已经尝试这样做:

console.log("GameOpsetning" + GameOpsetning);
GetSetup(Gameid);
console.log("GameOpsetning1" + GameOpsetning);
if (CheckGame()) {

那么控制台日志是:

GameOpsetning
GameOpsetning1
GameOpsetning3: 
post: done
GameOpsetning4: 
GetSetup : error

抱歉我的英语不好。

您正在测试 if($.post("game.php",... 这个 if 测试的结果是什么?总是 true。因为任何不是 false、0 或 undefined 的东西都是真的。您不是在测试 result 的 ajax 调用,而是在测试 an ajax call (一个函数).

if(GetSetup(Gameid))。 GetSetup(Gameid) returns : console.log("GameOpsetning4: " + GameOpsetning)if(GetSetup(Gameid)) 结果是 undefined,这解释了 GetSetup : error 输出。

在您的控制台中输入:if(console.log('test')) alert("yay"):它不会提醒 "yay",它只会记录 'test' 和 return 未定义。这是你的 if(GetSetup(Gameid)).

同样,如果您输入 if($.post("game.php")) alert('yay'),它会提醒您,因为测试总是正确的,无论 $.post.

的结果如何