通过Ajax请求调用页面时出现JavaScript错误

时间:2016-10-18 09:28:40

标签: javascript php jquery html ajax

如果我导航到页面http://localhost/abintegro/tests/sjt/1/index.html,我的页面工作正常但是当我通过Ajax调用页面时,我在inspect element console中收到此错误。

错误

  

未捕获的ReferenceError:未定义测试

at HTMLDocument.<anonymous> (<anonymous>:6:21)
at j (http://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js?_=1476785885327:2:29948)
at k (http://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js?_=1476785885327:2:30262)

index.html内如果我注释掉下面的函数错误不再存在,但是我需要该页面的功能才能工作。

我的功能

( function($, undefined) {

            $(function() {

                var test = new Test({

                    testName: "Situational Judgement Test 1",
                    dataURL : "./getresultshtml.php",
                    sendEmailURL: "./sendresultsbyemail.php",
                    contentFolder : "./",
                    solutionURL: "../../../content/f/id/21/",
                    userID: 0,
                    courseItemID: 42,
                    XMLFile: "exam.xml",
                    isStandalone: false
                });

                test.start();
            });
        }(jQuery));

script.js中的Ajax调用如下。

(一旦index.html被调用,我就会得到上面的错误

致电

  $("#test3").click(function (event) {
    $.post(
        "tests/sjt/1/index.html",
        function (data) {
            $('.stage2').html(data);
        }
    );
});

//////////////更新//////////////////////

测试是否在外部文件中使用

    function Test(settings){

    var defaults = {

        testName: "",
        dataURL: "",
        sendEmailURL: "",
        contentFolder: "",
        solutionURL: "",
        downloadURL: "",
        timesTaken: 0,
        userID: 0,
        courseItemID: 0,
        XMLFile: "",
        isStandalone: false
    };

    //merge defaults and settings
    this.settings = $.extend({}, defaults, settings);

    //module fields
    this.testData = null;

    //set up main test objects
    this.eventHub = new EventHub();

    this.Loader = new Loader(this.eventHub, this.settings);

}

Test.prototype.start = function(){

    var context = this;

    //load xml data and start test when data returns
    this.eventHub.subscribeOnce(this, "loader/xml", function(data){

        //get returned data object
        context.testData = data;
        context.settings.testType = data.testType;

        //initialise the test runner
        context.Runner = new Runner(context.eventHub, context.settings, context.testData);
        context.UI = new UI(context.eventHub, context.settings);
        context.Data = new Data(context.eventHub, context.settings);

        //start preload of images and subscribe to loaded event
        context.Loader.preloadImages(context.testData.questions);

        //show intro screen
        context.UI.prepareIntro(context.testData.introText);

    });

    this.Loader.loadXML(this.settings.XMLFile);

}

1 个答案:

答案 0 :(得分:0)

好的解决了这个问题。

我在http://localhost/abintegro/tests/sjt/1/index.html

中有js脚本CDN引用

当我在get.php内调用索引页面时,我只是将脚本调用添加到php文件而不是HTML文件中,它解决了问题。