javascript - Send null as a value in ngResource AngularJS -
i'm using angularjs 1.2.1's ngresource , i'm trying send null value in parameters in put request. unfortunately, when set, angular ignore parameter , not send it.
here's example code:
var user = $resource('/comments/:id', {id:'@id'}, { destroy: { method: 'put', params: {content: null} } }); var user = user.destroy({id:123}, function() {}); angular instead ignore content , not send key nor null value.
how can make angular send null?
as lèse has pointed out, javascript type null not valid value url parameter.
a javascript null value isn't same string 'null'. if wanted url /comments/123?content=null provide string 'null'. url parameters not json , null value in javascript doesn't mean same thing content=null because in latter case, null interpreted given server string value.
angular's $http service filters out null , undefined values in params object before constructing url them because there's no standardized way send undefined "value" server. doesn't make sense. however, if wanted send string 'undefined' or 'null' that's fine, require implementation on server side interpret such.
if you're making put request, why of data being json serialized within resource , of being sent via url parameter? maybe example code poorly represented, in example you're sending put request response body of {"id": 123} , url /comments/123. isn't redundant? why aren't sending content property , data in json string server? have null value you're looking because json strings can represent null type value. also, if you're deleting/destroying record, why aren't using delete request, instead aliasing destroy method put request?
so why not this? i'm not sure user has it...
var comment = $resource('/comments/:id', {id: @id}, { destroy: {method: 'put'} }); new comment({id: 123, content: null}).$destroy();
Comments
Post a Comment