admin管理员组文章数量:1415684
I add the physicalserver_id
field to my parameter object (params
), then send HTTP request:
var params = this.entityServer
params.extra_desc = JSON.stringify(this.entityServer.extra_desc)
params.physicalserver_id = undefined
debugger // there I have checked the `params.physicalserver_id`
this.$http.post('/api/xxx/add/', params).then( (response) => {
console.log(response, 'lml')
})
However, I get the error from backend, telling me that physicalserver_id
field is required.
In my browser, I check the Request Payload:
and find there is no physicalserver_id
.
if I give an existed number:
params.physicalserver_id = 8
Then, the problem disappeared.
EDIT-1
My question is, in my params I give the physicalserver_id=undefined
, but when I request the API, the Request Payload do not exist the physicalserver_id
, see my snapshot.
I add the physicalserver_id
field to my parameter object (params
), then send HTTP request:
var params = this.entityServer
params.extra_desc = JSON.stringify(this.entityServer.extra_desc)
params.physicalserver_id = undefined
debugger // there I have checked the `params.physicalserver_id`
this.$http.post('/api/xxx/add/', params).then( (response) => {
console.log(response, 'lml')
})
However, I get the error from backend, telling me that physicalserver_id
field is required.
In my browser, I check the Request Payload:
and find there is no physicalserver_id
.
if I give an existed number:
params.physicalserver_id = 8
Then, the problem disappeared.
EDIT-1
My question is, in my params I give the physicalserver_id=undefined
, but when I request the API, the Request Payload do not exist the physicalserver_id
, see my snapshot.
-
Better to
delete
properties from objects. But I don't understand your question, what exactly is the problem? Sounds like it might be an issue on the backend if the request is as expected? – CertainPerformance Commented Apr 25, 2018 at 3:50 -
Because you are setting it to
undefined
,undefined
values do not get included/sent. – Patrick Evans Commented Apr 25, 2018 at 3:51 - @PatrickEvans The original with this statement. now what should I set ? – qg_java_17137 Commented Apr 25, 2018 at 3:53
- It depends on the API what you need to set but you can try an empty string. – HMR Commented Apr 25, 2018 at 3:54
-
undefined and null value param will not sent to request ! Try
''
(empty) instead .. – Jack jdeoel Commented Apr 25, 2018 at 4:26
3 Answers
Reset to default 6This is the behaviour of JSON.stringify()
.
When you invoke this.$http.post('/api/xxx/add/', params)
, the frontend system (I guess you are using Angular) will convert params
to string via JSON.stringify()
. As params
is an object, all fields with undefined
value would be omitted in the final result string. Thus, the physicalserver_id
field would be missing in the request payload.
Refer to MDN for detail description:
If undefined, a Function, or a Symbol is encountered during conversion it is either omitted (when it is found in an object) or censored to null (when it is found in an array).
As @HMR mented, you need to check with API provider on "how to send empty physicalserver_id".
In my understanding, in the JavaScript undefined means nothingness.You set the parameters to not exist,the program will assume that there is no such parameter.In other words,the program can't find this parameter at all.So you must set a value for the parameter.
I was facing same issue, parameter with value of undefined wasn't being passed to backend api but with value of null, it was successfully being passed to backend endpoint. Its because null is a valid value for databases as values can be nullable but undefined is not a valid value
版权声明:本文标题:javascript - The undefined parameter property will be cleared away in the HTTP request payload - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745239806a2649259.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论