2

I am unable to make ajax context work:-

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 is always "" in this final if statement. The problem seems to be a scope issue but even one or both of the context statements make no difference.

Is it possible to resolve this scope issue? All advice appreciated.

1
  • 2
    context needs to be an OBJECT, not a primitive - see documentation Commented Dec 18, 2017 at 1:43

3 Answers 3

3

Ajax requests are asynchronous, meaning the rest of your program will execute before the request comes back. If you want to run code after the request has been completed, move it into the success function body.

You may also want to have a look at Promise.

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

1 Comment

Good points. I will check them out tomorrow as I have a crisis today :-(
0

If you want to force browser to finish execute first the request before continue reading the script, you just add async option in your ajax method and set it to false.

async: false

3 Comments

bad advice as synchronous xmlhttprequests on the main thread are deprecated :p
Useful for interim solution perhaps, while I figure out the async way to do it correctly. This is also helpful to see what the asynchronous version will require.
I was unable to get async: false to work in this situation :-(
0

The overall solution wasn't too complex once the asynchonosity was taken into consideration.

The main issue was to make a function for the error exits then waiting for the second success to be triggered to use the msgdata from the first ajax call, which was always in scope but only valid on the second ajax's successful trigger.

The joinfailed function could be inline so that joinplayer would still be in scope but I couldn't decide which error exit to put it on so I decided to keep it separate.

$("#gamefeed").on('click', ".chooseGame", function (evnt) {
  $.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); ?>)
    },
    success: function(data) {
      msgdata = data;
      $.ajax({
        type: "POST",
        url: "renamejoin.php",
        data: { 
          userID: "<?php echo $_SESSION['username']; ?>",
          joinID: joinplayer
        },
        success: function(data) {
          if (data != "") {
            joinfailed(joinplayer);
          } else {
            alert(msgdata); // "you joined joinplayer's game"
            // initialise comms and join the game
          }
        },
        error: function() {   
          joinfailed(joinplayer);
        }
      });
    },
    error: function() {
      joinfailed(joinplayer);
    }
  });
});
function joinfailed(joinplayer) {
  alert(joinplayer + "'s game is no longer available.");
  // start showing games awaiting players again
  gamefeedrefresh;
  timer = window.setInterval(gamefeedrefresh, 2500);
}

Comments

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.