如何在$ .toJSON之前或期间排除对象的id属性 [英] How to exclude an object's id property before or during $.toJSON

查看:150
本文介绍了如何在$ .toJSON之前或期间排除对象的id属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用django rest框架应用程序为余烬数据用户编写自定义REST适配器,并且需要构建JSON字符串来进行POST/PUT.唯一的问题是,在$.toJSON删除此方法之后,我似乎无法链接另一个jQuery方法.

I'm writing a custom REST adapter for ember-data users with a django rest framework app and need to build a JSON string to do POST/PUT. The only problem is I can't seem to chain another jQuery method after the $.toJSON that removes this.

到目前为止,我打算从中提取每个属性的ember.js对象,但我的django应用程序不希望id与HTTP POST一起使用

So far I have an ember.js object that I plan to extract each property from, except my django app doesn't want the id to go along with the HTTP POST

我可以得到一个合法的字符串

I can get a legit string like so

$.param(record.toJSON());

这将导致以下结果

id=0&username=dat

当我对JSON执行此操作时,我想要一种排除id的快速方法(使用手动编写它是否存在/无需手动编写?)使用jQuery 1.8 +

I'd like a quick way to exclude id when I do this toJSON (does this exist w/ out writing it by hand?) Using jQuery 1.8+

提前谢谢

推荐答案

事实证明这很简单

var json_data = record.toJSON();
delete json_data.id;
var data = $.param(json_data);

现在代替

id=0&username=dat

我现在得到

username=dat

这篇关于如何在$ .toJSON之前或期间排除对象的id属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆