doulao5916 2016-10-18 09:28
浏览 131

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

My page works fine if i navigate to the page http://localhost/abintegro/tests/sjt/1/index.html however when I call the page via Ajax I get this error in inspect element console.

Error

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)

within index.html if i comment out the function bellow the error is no more, however I need the function for the page to work.

My function

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

the Ajax call in script.js is as follows.

(once index.htmlis called this way I get the error above)

The Call

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

//////////////Update//////////////////////

Test if used in a external file

    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条回答 默认 最新

  • dongyan5706 2016-10-18 11:38
    关注

    Ok solved the problem.

    I had the js script CDN refrences within http://localhost/abintegro/tests/sjt/1/index.html

    as im calling the index page within get.php i just added the script calls to the php file rather the HTML file and it solved the problems.

    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程