1

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

updated code.. it should actually be console.log(reviewFunc()); because reviewFunc() is a function in the script being fetched and when type: "script" is defined or you use the short-hand $.getscript() the entire script is supposed to be read and evaluated at the global level so you can access it from other scripts. funny thing is i have this same thing (using the short-hand $.getScript(), calling a couple other scripts in another module of my code and it works perfectly yet when i tried to duplicate it it fails.
Only thing I can think of is maybe the script hasn't actually loaded, you could try specifying async: false, in the GET.
okay that gave me something new, It is giving me an Unexpected Token ILLEGAL error on the script i'm fetching on line 2. what do you think it is referring to? I don't see any illegal characters in there (second code block in the op) thanks
Check out: stackoverflow.com/questions/11444427/… That has happened to me as well, usually with jsFiddle.
that did it thanks so much i never would have figured that out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.