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

Get help using Construct 2

Post » Tue Mar 31, 2015 1:09 pm

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.
B
4
Posts: 7
Reputation: 214

Post » Tue Mar 31, 2015 1:35 pm

'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.
ImageImageImage
B
71
S
19
G
165
Posts: 3,425
Reputation: 95,561

Post » Tue Mar 31, 2015 1:47 pm

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
Who dares wins
B
50
S
10
G
10
Posts: 1,728
Reputation: 12,895

Post » Tue Mar 31, 2015 2:22 pm

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.
B
4
Posts: 7
Reputation: 214


Return to How do I....?

Who is online

Users browsing this forum: clissy09 and 8 guests