最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Send null as a value in ngResource AngularJS - Stack Overflow

programmeradmin0浏览0评论

I'm using AngularJS 1.2.1's ngResource and I'm trying to send null as a value in the parameters in a PUT request. Unfortunately, when it is set, Angular will ignore it the parameter and not send it.

Here's some example code:

var User = $resource('/ments/:id', {id:'@id'}, {
  destroy: { method: 'PUT', params: {content: null} }
});
var user = User.destroy({id:123}, function() {});

Angular will instead ignore content and not send the key nor the null value.

How can I make Angular send null?

I'm using AngularJS 1.2.1's ngResource and I'm trying to send null as a value in the parameters in a PUT request. Unfortunately, when it is set, Angular will ignore it the parameter and not send it.

Here's some example code:

var User = $resource('/ments/:id', {id:'@id'}, {
  destroy: { method: 'PUT', params: {content: null} }
});
var user = User.destroy({id:123}, function() {});

Angular will instead ignore content and not send the key nor the null value.

How can I make Angular send null?

Share Improve this question asked Mar 11, 2014 at 2:10 HengjieHengjie 4,6722 gold badges32 silver badges35 bronze badges 2
  • 5 Why are you trying to send null? All parameter values are sent as strings, so Angular wouldn't be able to send it anyway. – Lèse majesté Commented Aug 4, 2014 at 6:13
  • I was sending null because my API has an upsert endpoint so sending a content:null is supposed to destroy it instead. There were various reasons in terms of trade offs that meant that an upsert provided a cleaner API as opposed to a DESTROY request. – Hengjie Commented Aug 11, 2014 at 5:06
Add a ment  | 

2 Answers 2

Reset to default 5

As Lèse has pointed out, the javascript type null is not a valid value for a URL parameter.

A javascript null value isn't the same as the string 'null'. So if you wanted your URL to look like /ments/123?content=null just provide a string 'null'. URL parameters are not JSON and a null value in javascript doesn't mean the same thing as content=null because in the latter case, null would be interpreted by any given server as a string value.

Angular's $http service filters out null and undefined values in the params object before constructing a url with them because there's no standardized way to send undefined as a "value" to a server. It just doesn't make sense. However, if you wanted to send a string like 'undefined' or 'null' that's fine, but it require implementation on the server side to interpret it as such.

If you're making a PUT request, why is some of your data being JSON serialized within the resource and some of it being sent via a URL parameter? Maybe the example code is just poorly represented, but in that example you're sending a PUT request with the response body of {"id": 123} and the url /ments/123. Isn't that redundant? Why aren't you sending the content property and data in the JSON string to the server? Then it would actually have the null value you're looking for because JSON strings can represent a null type value. Also, if you're deleting/destroying a record, why aren't you using a DELETE request, but instead aliasing a destroy method with a PUT request?

So why not something like this? I'm not sure what the User has to do with it...

var Comment = $resource('/ments/:id', {id: @id}, {
  destroy: {method: 'PUT'}
});
new Comment({id: 123, content: null}).$destroy();

The ments that have been made since my question means that null shouldn't be sent as it cannot be properly encapsulated in HTTP params. However, it can be encapsulated in JSON. Why would anyone want to send a null value? Because sending null to an upsert [1] end point means destroy. Unfortunately, there is no way to define the default of content to be null in the .destroy() call.

The answer that I settled with was:

var Comment = $resource('/ments/:id', {id:'@id'}, {
  destroy: { method: 'PUT' }
});
var ment = Comment.destroy({id:123, content: null});

This is exactly like the @jbielick's answer and he does a fine job explaining the reason why it didn't work as a default param but works if it were provided as part of the input object to JSONify.

  1. Upsert is a merging of the words insert and update.
发布评论

评论列表(0)

  1. 暂无评论