Search code examples
javascriptjqueryajaxsyntax-errorimpactjs

syntax error being thrown when i use jQuery.ajax to fetch a script


I have the following code:

$.ajax({
    beforeSend: function () {
        console.log("fetching script");
    },
    url: "../../../scripts/review-level-" + ig.game.currentLevel.toString() + ".js",
    dataType: "script",
    type: "GET",
    success: function (script) {
        console.log(reviewFunc());
    },
    error: function (jqXHR, textStatus, errorThrown) {
        console.log(errorThrown);
    }
});

I am trying to fetch a script using jQuery.ajax, I've tried the shorthand function $.getScript() as well, and I am getting a SyntaxError as the errorThrown in the error parameter, even though under the network tab in the chrome developer tool it shows the script as being successfully fetched with a status code of 200. here is the script I am fetching:

function reviewFunc() {
    var overlay = ig.game.spawnEntity("EntityInfooverlay", 0, 0, {
        msg: {
            status: true,
            message: "Question #1:",
            counterOut: 0
        }
    });
    console.log("reviewFunc Finished");
    return overlay;
}

I can't see any syntax errors in there so i'm kinda stumped. If anyone see's anything i don't or has encountered this before any help would be greatly appreciated.


Solution

  • Might be easier to debug with some raw javascript:

    loadScript("../../../scripts/review-level-" + ig.game.currentLevel.toString() 
        + ".js", function() { reviewFunc(); });
    
    function loadScript(src, callback)
    {
        var s, r;
        r = false;
        s = document.createElement('script');
        s.type = 'text/javascript';
        s.src = src;
        s.onload = s.onreadystatechange = function() {
            if ( !r && (!this.readyState || this.readyState == 'complete') )
            {
                r = true;
                callback();
            }
        };
        document.body.appendChild(s);
    }
    

    EDIT

    Solution was found here.