9

When I do a $http.post in AngularJS with a object like:

{  name: '232', id: '3434', $type: "API.Models.Fields.ValuesList, API" }

with the signature:

$http.post('api/records', model);

the $type attribute is removed everytime on the chrome traffic listener no matter the value.

Is there some secret $ remover ;) ?

UPDATE: ANGULAR >= 1.3 NOW DOES NOT REMOVE THE $ ATTRS.

1 Answer 1

14

Yes, Angular strips dollar-prefixed properties when sending data over $http service.

$http service serialises objects to JSON string using angular.toJson method. This method strips properties with leading $ characters because angular uses this notation internally (e.g. instance method $save is available on all ngResource objects).

Quick workaround is to stringify the data manually (using JSON.stringify), before passing it on to $http:

$http.post('/api/path', JSON.stringify(model));
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Stewie. Odd that it strips them but i guess that makes sense given its native properties are usually prefixed with $
What if we need to send a regular expression or something? JSON.stringify doesn't allow that..
@Devin JSON format does not allow for RegExp, Function, Date, or even undefined. The only types allowed are number, object, array, true, false, null. See json.org for the full specification.
To be clear, you can still serialize these other types into strings, just be sure to be consistent and maintain serialization/deserialization functionality in parity on client/server.
FYI - This is not a problem w/ 1.3
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.