[英]AJAX sending object with functions throws error at function
我有以下Ajax
:
$.ajax({
type: 'POST',
url: '/Jeopardy/saveCategoryData',
dataType: 'json',
data: {
name: this.name,
questions: this.question_array,
sort_number: this.sort_number,
game_id: game_id
},
success: function (data)
{
this.id = data;
}
});
更新這是ajax所屬的完整類:
function Category(name, sort_number)
{
this.name = name;
this.sort_number = sort_number;
this.question_array = [];
this.id = 0;
/*
Functions
*/
}
Category.prototype.saveCategory = function()
{
$.ajax({
type: 'POST',
url: '/Jeopardy/createCategory',
dataType: 'json',
data: {
request: 'ajax',
name: this.name,
sort_number: this.sort_number,
game_id: game_id
},
success: function (data)
{
this.id = data;
}
});
$('.category_'+this.sort_number).each(function()
{
$(this).css('opacity', '1');
$(this).addClass('question');
})
}
Category.prototype.submitCategory = function()
{
$.ajax({
type: 'POST',
url: '/Jeopardy/saveCategoryData',
dataType: 'json',
data: {
request: 'ajax',
name: this.name,
questions: this.question_array,
sort_number: this.sort_number,
game_id: game_id
},
success: function (data)
{
this.id = data;
}
});
}
Category.prototype.addQuestion = function(question,index)
{
this.question_array[index] = question
}
其中this.question_array是question
對象的數組:
function Question(question, score)
{
this.question = question;
this.score = score;
this.answer = [];
}
Question.prototype.getScore = function()
{
return this.score;
}
Question.prototype.addAnswer = function(answer)
{
this.answer.push(answer)
}
我的回答對象:
function Answer(answer, is_correct)
{
this.answer = answer;
this.is_correct = is_correct;
}
當我的Ajax
提交時,我在函數addAnswer
收到一條錯誤addAnswer
: Cannot read property 'push' of undefined
誰能告訴我為什么會這樣(我對JavaScript中的OOP還是很陌生)
更新create.js(控制對象的腳本)
保存問題功能:
function saveQuestion() {
var question = new Question($('#txt_question').val(), current_money);
var array_index = (current_money / 100) - 1;
$('.txt_answer').each(function ()
{
var answer = new Answer($(this).val(), $(this).prev().find('input').is(':checked'));
question.addAnswer(answer); // <-- Add answer
})
if(current_element.find('.badge').length == 0)
{
current_element.prepend('<span class="badge badge-sm up bg-success m-l-n-sm count pull-right" style="top: 0.5px;"><i class="fa fa-check"></i></span>');
}
addQuestionToCategory(question, array_index);
questionObject.fadeOutAnimation();
}
function addQuestionToCategory(question, index) {
switch (current_category_id) {
case "1":
category_1.addQuestion(question, index);
break;
case "2":
category_2.addQuestion(question, index);
break;
case "3":
category_3.addQuestion(question, index);
break;
case "4":
category_4.addQuestion(question, index);
break;
}
}
以及在每個類別對象上調用ajax的函數:
function saveGame()
{
category_1.submitCategory();
category_2.submitCategory();
category_3.submitCategory();
category_4.submitCategory();
}
調試調用棧:
Question.addAnswer (question.js:25)
n.param.e (jquery-1.11.0.min.js:4)
Wc (jquery-1.11.0.min.js:4)
Wc (jquery-1.11.0.min.js:4)
(anonymous function) (jquery-1.11.0.min.js:4)
n.extend.each (jquery-1.11.0.min.js:2)
Wc (jquery-1.11.0.min.js:4)
n.param (jquery-1.11.0.min.js:4)
n.extend.ajax (jquery-1.11.0.min.js:4)
Category.submitCategory (category.js:47)
saveGame (create.js:116)
onclick (create?game_id=1:182)
*更新
好吧,如果我將addAnswer函數更改為以下內容,那將會發生一些奇怪的事情:
Question.prototype.addAnswer = function(answer)
{
if(this.answers != undefined)
{
this.answers.push(answer)
}
}
工作正常嗎?
看來亞歷山大刪除了他的答復,這是我的建議:
function Question(question, score)
{
this.question = question;
this.score = score;
this.answer = [];
}
Question.prototype.getScore = function()
{
return this.score;
}
Question.prototype.submitQuestion = function()
{
}
Question.prototype.addAnswer = function(answer)
{
this.answer.push(answer)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.