0 Favourites

How to get AJAX 'On Completed' for POST evaluate to true

  • I have a function that AJAX POSTs to a .php page to update a DB. I then immediately AJAX GET new results from the DB, however many times the POST hasn't completed and my GET results are incorrect. (even though the update was successful, I believe it's a timing issue.)

    I have tried AJAX 'On myPostTagName Completed', however I am not able to get it to evaluate to true. it stalls at that function. I have tried having my post.php page echo "1" back and test for that in .LastData but that again doesn't seem to work.

    What does C2s AJAX 'On zyx Completed' look for to evaluate to true and how do I get it (or my .php page) to respond appropriately to make it do so?

    Thanks for any assistance.

  • 'On X' events are triggers, which are callbacks for asynchronous events. It sounds like you've placed it sequentially in your code, which won't work. You need to post your message then just return. At the top level have any 'On X' events and when they are called some (short) time in the future, you continue your processing there. That's why there are 'On completed', 'On error', and 'On progress' events, to handle the asynchronous nature of the call.

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • It does not evaluate to true, it gives you the feedback of the ajax call, which you can use for true or false analysis.

    ie

    Ajax request http://somesite/index.php?update=addDBthingy tag "updating"

    On your server, when the php script has run, it might echo back the result of the adding, for instance echo "success";

    Then, in C2, when you have

    event

    On request complete "updating"

    subevent

    compare 2 variables

    Ajax.LastData = "Success"

    action set some boolean = true

    Else

    action set some boolean = false

  • Thanks for your help folks.

    In noodleing 'blackhornet's response, I took a look at my nesting and figured out that my 'On myPostTag Completed' function, which is part of my end-game tree, was nested under my 'game_running' tree, which at first glance makes sense (if it were processed synchronously) but due to asynchronous processing, it became unavailable as the 'game_running' tree became disabled as part of the same end-game tree.

    To fix my issue all I needed to do was move the 'On myPostTag Completed' tree out of the 'game_running' tree to root of the project which made it perpetually available.

    Thanks for the assistance.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)