Software Archive
Read-only legacy content
17061 Discussions

Problems when trapping 404 (Not Found) error

Derek_M_1
Beginner
203 Views

Hi

I've created a project using Intel XDK (3619) | App Designer | HTML + Cordova template - target iPhone / Android.

In my app, I want to trap errors when clicking a non-existent link (404) in order to find the missing target name. In a Test project with just two buttons (one which links to a sub-page and one to a non-existent link), The page link works fine; the other, as expected, generates an error message in the simulation console:

"GET http://localhost:39875/xxx.html 404 (Not Found)"

I can "trap" the error using .ajaxError which I put inside of the auto generated code:

/*jshint browser:true */
/*global $ */(function()
{
 "use strict";
 /*
   hook up event handlers 
 */
 function register_event_handlers()
 {
     $(document).ajaxError(function(event, jqxhr, settings, thrownError) {
        var msg = settings.url;
        navigator.notification.alert("Error "+ msg);
     });

         
     /* button  Button */
    $(document).on("click", ".uib_w_2", function(evt)
    {
         /*global activate_subpage */
         activate_subpage("#uib_page_1"); 
         return false;
    });   
    }
 document.addEventListener("app.Ready", register_event_handlers, false);
})();

However, when triggered the error event returns "undefined". There is no data in "jqxhr", "settings" or "thrownError". The "event" object has mostly "undefined". Presumably some timing issue???
I've tried many variations, including using .onerror (didn't trigger at all) with no luck. Is there another way to determine the missing target (xxx.html)?
Any ideas!
Thanks
Dunum (newbie!)

ps. apologies if this is not the correct forum - please advise other forums I should try.

0 Kudos
3 Replies
PaulF_IntelCorp
Employee
203 Views

I'm not clear on how you would detect this. Part of the problem is you're not dealing with access to a traditional web server, there is no web server. All the pages you reference (assuming you are referencing them by name as if they were part of your project) are file:// references, not http:// references.

In general, building a "multi-page" Cordova app is not recommended. Single-page apps are the recommended procedure. See this Cordova doc page for a little more background > https://cordova.apache.org/docs/en/latest/guide/next/

0 Kudos
Derek_M_1
Beginner
203 Views

Hi Paul, thanks for your response.

My app is actually a single-page app. It is an encyclopaedia type app for iPhone and Android with many content pages (maybe several hundred). I have a simple front end and a content page template (within the single app page) into which I “insert” data (DIVs for “title” and “content”) which I then activate with some simple code. The content is loaded from a SQLite db as text. The issue arose because some content pages may contain links to other content pages. My problem was how to load new content data from a "link" clicked in data already being displayed in the template. The option I was exploring was to embed a non-existent link (e.g. <a href="xxx.html">xxx</a>) in the text field, and use .ajaxError to trap the error when it was clicked; then process the re-loading of the template using data determined by the name (url) of the non-existent link. The trap worked but, as I said,  the error event returned "undefined" - so nothing telling me where to link to. I've now looked at an alternative option which is to embed an onclick function (e.g. <a onclick="showTarget('xxx')">xxx</a>) in the text file -  'xxx' is passed to the function from which I can determine the page to show.

I guess there may be alternitive ways of achieving this requirement but this seems to work ok for me:-)

0 Kudos
PaulF_IntelCorp
Employee
203 Views

Derek -- thanks for the clarification. Sounds like you've found a reasonable alternative to the approach you were using.

0 Kudos
Reply