0

I am creating Stratego.
(the error is farther down)
so i am creating the map when the page is loadet whit:

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

and then i do some console log to make sure it works..

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

and here the error comes, it will open the file "game.php" whit no error. but it will not do eny thing in side the post/funcion. It will not do the alert or the if or 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);

}

the console output:

GameOpsetning
GameOpsetning3: 
post: done
GameOpsetning4: 
GetSetup : error

there is no console errors and the page is return JSON

and it have try ed to do like:

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

then the console log is:

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

Sorry for my bad English.

1 Answer 1

1

You're testing if($.post("game.php",... What is the result of this if test? Always true. Because anything that is not false, 0 or undefined is true. You're not testing the result of an ajax call, you're testing an ajax call (a function).

Same with if(GetSetup(Gameid)). GetSetup(Gameid) returns : console.log("GameOpsetning4: " + GameOpsetning). if(GetSetup(Gameid)) result is undefined, which explains the GetSetup : error output.

Type this in your console : if(console.log('test')) alert("yay") : it won't alert "yay", it will just log 'test' and return undefined. This is your if(GetSetup(Gameid)).

Same way, if you type if($.post("game.php")) alert('yay'), it will alert yay, because the test is always true, whatever the result of the $.post.

Sign up to request clarification or add additional context in comments.

3 Comments

that make sense. I have allow try ed whit out the if. same result. from if ($.post("game.php", { gameid: Gameid }, function(data) { to $.post("game.php", { gameid: Gameid }, function(data) { then it have to do the if that have a return? jsfiddle.net/7nq9wmq6
Yes, first make the ajax call, when the response comes back, test it. Place the if in the callback function.
i addet .post("game.php", {.....).fail(ajaxError); function ajaxError(jqXHR, textStatus, errorThrown) { alert('$.post error: ' + textStatus + ' : ' + errorThrown); }; and find the error. it was a , to much. But now the console output jsfiddle.net/888mkcy7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.