Получение "ReferenceError не определено"

Я перебираю некоторые объекты викторины и сохраняю результаты в переменные 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));

}

Ошибка начинается в строке 14 сразу после g_ListQuizzes

Код строки 14: var arrayLength = g_listQuizzes[g_quizId].arrQuestions.length;

и виновник выглядит так: [g_quizId]

Есть идеи?

1 ответ

jsonQuizData не определено. Инициализируйте это с var jsonQuizData={} прежде чем положить вещи в нем.

И ради воплей, люди, перестань все жуликать. Просто отправьте JSON и работайте с JSON. Я никогда не совсем понимал эту очень странную форму строчкования.

Другие вопросы по тегам