Ajax on error not firing - can you help me figure out why?

0 favourites
  • 8 posts
From the Asset Store
7 Errors
$15 USD
7 Errors is a game where you have to find the 7 mistakes before time runs out. Can you find all 7? Have fun looking.
  • I used Construct to build a "dashboard" for accessing several device GUIs for a system I maintain. In short, it loads a page of buttons on a local machine which open browser windows to the respective device URLs when clicked - all of this works great.

    I want to add an "online status" feature which changes a sprite's color for each device when the dashboard is launched (green = online, red = offline). In attempts to accomplish this, I have the program make an AJAX call to the respective devices when each button is created and will attempt to use the AJAX.onerror condition to set the status. (I realize I may have CORS issues, but that will be the next hurdle.)

    The issue I am having is getting the AJAX.onerror condition to fire. It is not firing (I've tested with oncomplete, onprogress, etc, just in case there simply was no "error" being reported). I've even tried forcing an error with a bad URL and attempting to force a complete call by calling my own server which I know will respond.

    My logic works like this:

    The dashboard buttons (links) are created from a JSON file. When the button is created, it stores a unique identifier for that button (I'm using the URL that is in the JSON file for that device) in an instance variable for the "statusLED" sprite I create which is also used as the "tag" for that buttons's status AJAX call. (This ends up looking something like "schttp://xxx.xxx.xxx", "schttp://xxx.xxx.yyy", etc.)

    Later on in the script - outside of the button-creation logic - I have a For Loop that cycles through all of the "statusLED" sprites on the page and for each of them, I am checking the AJAX onError and using that "statusLed" sprite's instance variable in the "tag" field. The idea here is that the default "statusLed" is green (sprite frame 1) and if an AJAX error occurs (no response from server device) it will set the frame to red (frame 0). I've tried "onComplete", "onanyErrors", "onanyComplete" just to see if I was simply not getting an error, but those never fire either.

    To test whether or not it is firing, I have the value of the instance variable for each "statusLED" sprite appended to a text field but it doesn't happen. When I move this "append" action outside of the onError block and just inside the For Loop, the instance variables populate the box - so I know the issue is not in the setting / recalling of the tag names.

    Any ideas what I am getting wrong here? Is the AJAX.onError not inside the ForLoop? (and if not, how else can I check multiple independent Ajax calls?)

    I have other AJAX calls to pull data in from my JSON files in other parts of the script and they are working just fine.

    (I only recently added the "statusCheck" condition as I realized this section was looping continuously - didn't help. - Also not exactly sure if the For Loop executed before the "statusCheck" var is set to 1.)

    Thank you in advance!

    Tagged:

  • I don't think anyone could help with those slim screenshots with the loops missing, we would need the rest of the events and the ability to test it.

  • Check the console log - sometimes AJAX throws an error there instead of triggering "On Error" event. Also I would probably move "On any error" event out of the loop. It should be a top-level event. You can use AJAX.tag expression to get the tag of the failed request.

  • lionz - I can share more, the rest of the code for that section is all just creating the button and some device information text from fields in the JSON file as well as changing the images used in the button based on data from the JSON file - I didn't think it was pertinent to the issue.

    dop2000 - I will look at the console log now and see if I can spot something. I know that, generally, the AJAX stuff should just be in the timeline as it fires asynchronously but I don't know how else to check for the multiple ajax calls in order to associate them with the respective "statusLed" sprites.

    The way I have this working is that I have "device category" buttons at the top of the screen. (for example "Encoders," "Processors," "Cameras"). Clicking on the category button destroys any existing elements (the link buttons and related images and text) and then creates new ones for that category (eg "Encoder 1," "Encoder 2"). When the new ones load, that is when I am trying to make the AJAX call to its URL to test if the device is online.

    So, if for example, there are 4 devices in that category (ie Encoder 1 at 10.11.12.13, Encoder 2 at 10.11.12.14 and so on), I am making 4 separate AJAX calls.

    How else could I check for errors in each of those specific AJAX calls without putting them in a loop (or hard-coding an AJAX.onError for every potential device)?

    (I suppose I could use a global "Tag variable" that advances every time an AJAX response completes or errors) so on the next loop of the script it is checking the *next* call - if that could make a difference, I certainly can try it).

    (NOTE: In the screenshot below, the AJAX request I'm using to test the status -Section 22, 5th Action- shows "" for the URL... I was just trying to force an error by using a blank URL and forgot to change it back before I took the screenshot. Normally that URL would be the variable "thisDeviceUrl" which is set at the top of line 21.)

    Here are broader screenshots:

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • A Clue!

    dop2000 - So I just ran it a few times looking at the console.

    When I used onError, I get this in the console:

    I confirmed these two lines are referencing my "status check" AJAX calls by disabling / enabling the call in my script. Since I am previewing, it appears that the call is actually being made to Scirra's servers (preview.construct.net/local.html) instead of my actual URL and is completing successfully as a result.

    So I changed my condition to onComplete, checked the Console again and found this!

    It would appear that even though Construct let me put the AJAX.onError inside the loop, it doesn't actually want me to do that. I believe this could certainly explain why my actions aren't firing.

    So I guess now I have to figure out another way to get it to check the result for each of my individual AJAX calls. (I will my idea of advancing a variable).

    If you or anyone has a good way of doing this, I would be greatly appreciative.

    Thank you!

  • So I guess now I have to figure out another way to get it to check the result for each of my individual AJAX calls.

    Like I said, you can use AJAX.tag expression inside the "On any error" trigger to see which request has failed.

  • Would I do this by using an "if statement" (sub-event)?

    Put a for-loop inside the On Any Error as a sub-event and then in that for-loop do:

    if AJAX.tag == statusLed.ledID

    ?

  • YES! That did the trick! Thank you very much!!!

    How does one get their brain to think of those alternative work-arounds? LOL

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