【发布时间】:2015-08-21 19:10:30
【问题描述】:
我的问题标题可能措辞不好,抱歉。
我想创建一个 Javascript 类来简化向 php 页面发送信息的过程。
我正在使用this answer 中描述的方法来创建我的类
这是我目前所拥有的:
var ParseObject = Class.extend({
init: function(classname, id){
// required properties
this.classname=classname;
this.objects=[];
this.fields=[];
// optional properties
if(id && id != '') this.id='';
//this.command = command;
//this.callback='';
//this.parent=[];
//this.children=[];
//this.relation='';
},
set: function(field, value) {
if(field == 'classname') this.classname=value;
else if(field == 'callback') this.callback=value;
else if(field == 'command') this.command=value;
else this.fields.push(field, value);
},
addChild: function(obj){
this.children ? this.children.push(obj) : this.children= [obj];
},
addParent: function(linkedFieldName, parentClassName, parentId){
this.parent=[linkedFieldName, parentClassName, parentId];
},
addObject: function(obj){
this.objects.push(obj);
},
isRelatedBy: function(relation){
this.relation=relation;
},
send: function(){
var obj = this;
$.ajax({
type: "POST",
dataType: "json",
url: "php/parseFunctions.php",
data: {data:obj},
success: function(response) {
// do stuff
},
error: function(response) {
// do stuff
}
});
}
});
这就是我尝试使用该类的方式:
var testObject = new ParseObject('TestObject');
testObject.set('callback','testResopnse');
testObject.set('command','save');
testObject.set('title','New Title');
testObject.set('description','New Description');
testObject.set('stuff',['one','two','three']);
testObject.addParent(['parent', 'MeComment', 'kRh5xcpkhz']);
testObject.send();
在我到达testObject.send();之前,一切都按预期进行
我希望将以下对象发送到我的 PHP 页面:
但相反,我得到的是“未捕获 RangeError:超出最大调用堆栈大小”
为什么会发生这种情况,我怎样才能达到预期的效果?
更新:#
根据昆汀的建议,这让我得到了整理
var obj =$.parseJSON(JSON.stringify(this));
【问题讨论】:
-
@GobSmack — 问题中没有任何内容表明 Ajax 调用应该发送 JSON。 PHP 默认不会解析 JSON。
-
你能贴出你的PHP页面片段吗?
-
你的
addParent函数需要 3 个参数,你只是传递给它一个数组。但这不应该导致错误。 -
@blex 谢谢,不是问题,但我确实需要解决这个问题
标签: javascript jquery ajax oop