How to exclude an object's id property before or during $.toJSON

[亡魂溺海] 提交于 2019-12-25 01:22:23

问题


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.

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());

This results in the following

id=0&username=dat

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+

Thank you in advance


回答1:


Turns out this was really simple

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

Now instead of

id=0&username=dat

I now get

username=dat


来源:https://stackoverflow.com/questions/12267796/how-to-exclude-an-objects-id-property-before-or-during-tojson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!