发送null作为在ngResource AngularJS值 [英] Send null as a value in ngResource AngularJS
问题描述
我用AngularJS 1.2.1的ngResource,我要发送空
作为一个PUT请求的参数值。不幸的是,当它被设置,角将忽略它的参数,而不是发送。
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.
下面是一些例子code:
Here's some example code:
var User = $resource('/comments/: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?
推荐答案
由于冒犯指出,在JavaScript类型空
不是一个URL参数的有效值。
As Lèse has pointed out, the javascript type null
is not a valid value for a URL parameter.
JavaScript的空
的价值是不一样的字符串'空'
。所以,如果你想你的URL看起来像 /评论/ 123?内容= NULL
只提供一个字符串'空'
。 URL参数并不JSON和JavaScript中的一个空
值并不意味着同样的事情,内容= NULL
因为在后者的情况下,空将是由任何给定的服务器作为字符串值PTED间$ P $。
A javascript null
value isn't the same as the string 'null'
. So if you wanted your URL to look like /comments/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.
角的$ HTTP服务过滤掉无效和构造URL与他们的,因为有送未定义
作为价值的服务器没有标准化的方法之前,params对象未定义的值。它只是没有任何意义。但是,如果你想送就像一个字符串未定义
或'空'
这很好,但它需要实施在服务器端间$ P $角它是这样。
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.
如果你正在做一个 PUT
的要求,为什么你的一些数据被JSON的资源中序列化和它的一些通过URL参数发送?也许例如code是只是不好再presented,但在这个例子中你发送一个 PUT
与响应主体请求 {ID:123}
和网址 /评论/ 123
。是不是多余的?你为什么不发送的JSON字符串到服务器的内容
属性和数据?然后,它实际上有空
值你要找的,因为JSON字符串的可以再present一个空
类型值。此外,如果你删除/销毁的记录,你为什么不使用删除
的要求,而是混叠一个带PUT请求destroy方法?
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 /comments/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('/comments/:id', {id: @id}, {
destroy: {method: 'PUT'}
});
new Comment({id: 123, content: null}).$destroy();
这篇关于发送null作为在ngResource AngularJS值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!