Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS $http.post removing attributes

Tags:

angularjs

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.

like image 488
amcdnl Avatar asked Dec 03 '13 00:12

amcdnl


1 Answers

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));
like image 166
Stewie Avatar answered Nov 04 '22 04:11

Stewie