通过Ajax请求调用页面时出现JavaScript错误
JavaScript errors when calling a page through a Ajax request
如果我导航到页面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文件,它解决了问题。
相关文章:
- JavaScript错误:Microsoft JScript运行时错误:应为对象
- 将字符串拆分为数组时出现JavaScript错误
- 处理JSON字符串会导致JavaScript错误
- 使用AJAX在剑道网格中分组时出现Javascript错误
- Ajax对web URL的调用导致javascript错误
- 为什么当我尝试在Ionic中使用列表时会出现此JavaScript错误
- 可以't解决Javascript错误
- django出现神秘的javascript错误
- JavaScript错误无效的限定符
- 使用主题运行时portlet liferay时出现Javascript错误
- 绑定函数时在IE7中未实现Javascript错误
- Javascript错误:未捕获引用错误:未定义编辑
- eventListener出现JavaScript错误
- sharepoint站点的internet explorer中出现Javascript错误
- LoadControl HttpCompileException自定义服务器控件JavaScript错误
- 销毁Bootstrap popover时出现Javascript错误
- 无法加载Javascript错误XMLHttpRequest
- 使用JSON.parse时出现无效字符javascript错误
- 修复Javascript错误
- 模板字符串上的netbeans javascript错误(backticks)