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.

Joe
  • 1,326
  • 7
  • 34
  • 52

1 Answers1

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.

Community
  • 1
  • 1
Anders
  • 15,227
  • 5
  • 32
  • 42
  • 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. – Joe Nov 30 '12 at 17:25
  • Only thing I can think of is maybe the script hasn't actually loaded, you could try specifying `async: false,` in the `GET`. – Anders Nov 30 '12 at 17:44
  • 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 – Joe Nov 30 '12 at 21:22
  • Check out: http://stackoverflow.com/questions/11444427/javascript-uncaught-syntaxerror-unexpected-token That has happened to me as well, usually with jsFiddle. – Anders Nov 30 '12 at 21:33
  • that did it thanks so much i never would have figured that out – Joe Nov 30 '12 at 22:05