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.

Share Improve this question edited Jan 5, 2022 at 7:49 qg_java_17137 asked Apr 25, 2018 at 3:47 qg_java_17137qg_java_17137 3,60916 gold badges45 silver badges90 bronze badges 5
  • 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
Add a ment  | 

3 Answers 3

Reset to default 6

This 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

本文标签: javascriptThe undefined parameter property will be cleared away in the HTTP request payloadStack Overflow