带有函数的 AJAX 发送对象会在函数处抛出错误
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
出现错误,说:Cannot read property 'push' of undefined
谁能告诉我为什么会发生这种情况(我对 JavaScript 中的 OOP 相当陌生)
更新创建.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)
}
相关文章:
- 尝试调用AngularJS上的函数时出错
- 得到"未定义不是函数“;使用显示原型图案时出错
- 从函数中的函数返回数组时出错
- 从 java 脚本调用函数时 PHP 中出错
- 使用PHP和Javascript在函数内部传递alphaneumeric值作为参数时出错
- 比较$watch函数内的基元类型时出错
- CakePHP错误:在非对象上调用成员函数get(),使用cakejavascript助手时出错
- 构造函数出错
- javascript函数出错
- AngularJS:没有定义$rootscope的全局函数出错,没有定义$scope
- Java脚本函数不;一个函数出错后无法工作
- OnClick函数出错
- 函数b()出错
- for循环中的JavaScript匿名函数出错
- 使用带有href的attr函数出错
- 从外部文件调用JavaScript函数出错
- 使用本地存储的可排序列表函数出错
- 确定数字中位数的函数出错
- PHP邮件函数发送电子邮件,但AJAX函数出错
- ajax函数出错,尽管它转到了我的webservice