gpt4 book ai didi

javascript - 使用 jquery 使用方法发布对象

转载 作者:行者123 更新时间:2023-11-30 12:25:33 25 4
gpt4 key购买 nike

我有一个简单的对象

function MyClass()
{
this.id = null;
// ... Lot of stuff in it
}
MyClass.prototype.parse = function(text)
{
// A parsing function that fills the object
}

如果我尝试使用 $.post 发送对象

$.post("target.php", { QUERY: "query_id", object: obj }, function(data){
console.log(data);
}, 'json');

objMyClass 的一个实例,我得到一个错误:text is undefined

我如何让它明白我只想要发送数据,而不是像 parse 这样的方法,因为它没有任何意义?

最佳答案

您可以通过以下方式遍历对象的属性

for (var prop in obj)

将其与 typeof 结合使用以确定属性是否为函数,您可以创建一个函数以将所有非函数属性作为新对象返回。我以这个 jsfiddle 为例。

http://jsfiddle.net/6w0wj8hj/

这意味着你可以做

$.post("target.php", { QUERY: "query_id", object: obj.getProperties() }, function(data){
console.log(data);
}, 'json');

关于javascript - 使用 jquery 使用方法发布对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29528346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com