admin管理员组

文章数量:1200394

Here I have this.state.word is boolean type so value is in true/false

but when I want to try to append this.state.word it gives me error like this argument type boolean is not assignable to parameter type string | blob

so how to append boolean type values(true/false) in formdata( NOTE :- I want to send in boolean type not in string) ?

handleSendSynopsis() {
  const data = new FormData();
  data.append('word', this.state.word);
}

Here I have this.state.word is boolean type so value is in true/false

but when I want to try to append this.state.word it gives me error like this argument type boolean is not assignable to parameter type string | blob

so how to append boolean type values(true/false) in formdata( NOTE :- I want to send in boolean type not in string) ?

handleSendSynopsis() {
  const data = new FormData();
  data.append('word', this.state.word);
}
Share Improve this question edited Feb 7, 2019 at 7:10 asked Feb 7, 2019 at 7:07 user10950990user10950990 4
  • try converting it to string like this: String(this.state.word) – Vaibhav Vishal Commented Feb 7, 2019 at 7:09
  • @VaibhavVishal I want to send in boolean type not in string – user10950990 Commented Feb 7, 2019 at 7:11
  • but it wants a string. – Vaibhav Vishal Commented Feb 7, 2019 at 7:15
  • Does this answer your question? FormData sends boolean as string to server – Adam Commented Jun 29, 2020 at 8:53
Add a comment  | 

2 Answers 2

Reset to default 16

use JSON.stringify on the client to send numbers and boolean values, then parse it on the backend

For Example

const form = new FormData;
const data = {
    name: 'john doe',
    active: true,
    count: 42
};

form .append('file', file); // send your file here
form .append('fileProps', JSON.stringify(data));

According to FormData Documentation, FormData.append accepts only a USVString or a Blob. S you will have to convert your data to string and then parse it later on the backend. You can use JSON.stringify to convert your form object to a string.

本文标签: javascriptHow to append boolean type values in formdataStack Overflow