0

I'm attempting to load json files as soon as the page loads. Those json files are contained within a folder, and they serve as the foundation around which the programme is constructed (each json file represents a page). The issue is that I want the script to be able to handle a random amount of json files, which is not possible.

For the time being, I have the following recursive code that does precisely what I need it to do: it loads each page and, in the event of an error, it runs the application's startup procedure. I'm having trouble since my browser is returning a 404 error, which is understandable given the situation. I'm wondering whether there is a method to catch the 404 error (which signals that there are no more json files to be loaded) without the error being displayed in the browser.

This link doesn't help (Catch a 404 error for XHR)


$(document).ready(function() {
    let pageArray = [];
    loadPages(1, pageArray);

    function loadPages(nb, array) {
        $.ajax({
            url: 'configuration/pages/page' + nb + '.json',
            dataType: 'json',
            error: function() {
                initialize(array);
            },
            success: function(data) {
                array.push(data);
                loadPages(nb + 1, array);
            }
        });
    }

    function initialize(pages) {
        console.log(pages);
    }
});```

0 Answers0