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

JavaScript errors when calling a page through a Ajax request

本文关键字:JavaScript 错误 Ajax 请求 调用 通过      更新时间:2023-09-26

如果我导航到页面http://localhost/abintegro/tests/sjt/1/index.html,我的页面工作正常,但是当我通过Ajax调用页面时,我在检查元素控制台得到这个错误。

误差

Uncaught ReferenceError: Test is not defined

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);
}

Ok,问题解决了。

我有js脚本CDN引用在http://localhost/abintegro/tests/sjt/1/index.html

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