我正在循环一些测验对象并将结果存储到json变量中。收到"ReferenceError is not defined"
错误但不确定原因。
JS代码
// This function will send the quiz results to a PHP script that will store the quiz results.
function storeQuiz(){
//setup the json object
var jsonQuizData = new Object();
// get the learners info
var sl = GetPlayer();
var learnersName = sl.GetVar("usersName");
var learnersEmail = sl.GetVar("usersEmail");
//var learnersCompany = sl.GetVar("usersCompany");
// Loop through the quiz object to go through each quiz question and storying the results.
var arrayLength = g_listQuizzes[g_quizId].arrQuestions.length;
jsonQuizData["questionCount"] = g_listQuizzes[g_quizId].arrQuestions.length;
jsonQuizData["strQuizName"] = g_listQuizzes[g_quizId].strQuizName;
jsonQuizData["nPassingScore"] = g_listQuizzes[g_quizId].nPassingScore;
jsonQuizData["dtmFinished"] = g_listQuizzes[g_quizId].dtmFinished;
jsonQuizData["nMaxScore"] = g_listQuizzes[g_quizId].nMaxScore;
jsonQuizData["nMinScore"] = g_listQuizzes[g_quizId].nMinScore;
jsonQuizData["nPtScore"] = g_listQuizzes[g_quizId].nPtScore;
jsonQuizData["strQuizId"] = g_quizId;
jsonQuizData["strStatus"] = g_listQuizzes[g_quizId].strStatus;
jsonQuizData["usersName"] = learnersName;
jsonQuizData["usersEmail"] = learnersEmail;
//jsonQuizData["usersCompany"] = learnersCompany;
for(var i = 0; i < arrayLength; i++){
jsonQuizData["question"+i] = {
"description" : g_listQuizzes[g_quizId].arrQuestions[i].strDescription,
"strCorrectResponse" : g_listQuizzes[g_quizId].arrQuestions[i].strCorrectResponse,
"strStatus" : g_listQuizzes[g_quizId].arrQuestions[i].strStatus,
"strUserResponse" : g_listQuizzes[g_quizId].arrQuestions[i].strUserResponse,
"nWeight" : g_listQuizzes[g_quizId].arrQuestions[i].nWeight,
"nPoints" : g_listQuizzes[g_quizId].arrQuestions[i].nPoints,
"nQuestionNumber" : g_listQuizzes[g_quizId].arrQuestions[i].nQuestionNumber
}
}
console.log(jsonQuizData);
// Send data to handler
request = new XMLHttpRequest();
request.open("POST", "http://foo.com/quizResultsHandler.php", true);
request.setRequestHeader("Content-type", "application/json");
request.send(JSON.stringify(jsonQuizData));
}
错误从g_ListQuizzes
第14行代码:var arrayLength = g_listQuizzes[g_quizId].arrQuestions.length;
并且罪魁祸首看起来是:[g_quizId]
有什么想法吗?
答案 0 :(得分:0)
jsonQuizData
未定义。在将内容放入其中之前,先使用var jsonQuizData={}
对其进行初始化。
对于大声喊叫,人们不要把所有东西都串起来。只需发送JSON并使用JSON即可。我从未完全理解这种非常奇特的弦乐化时尚。