javascript帖子不做功能但加载url

javascript post not doing function but load url

本文关键字:加载 url 功能 javascript      更新时间:2024-03-17

我正在创建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

没有控制台错误,页面返回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或未定义的都是true。您不是在测试ajax调用的结果,而是在测试ajax调用(一个函数)。

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

在控制台中键入以下内容:if(console.log('test')) alert("yay"):它不会提醒"yay",只会记录"test"并返回undefined。这是您的if(GetSetup(Gameid))

同样,如果您键入if($.post("game.php")) alert('yay'),它会提醒yay,因为无论$.post的结果如何,测试始终为true。