[英]post api request with json input fields in reactjs
i have form with three input fields with type as我有三个输入字段的表单,类型为
i have design the form but how to send data as object in gcloud field?我已经设计了表单,但是如何在 gcloud 字段中将数据发送为 object? code for gcloud input field:
gcloud 输入字段的代码:
<label>gcloud keyfile json</label>
<TextareaAutosize
name="gcloud_keyfile"
type="text"
id="gcloud_keyfile"
value={values.gcloud_keyfile}
onChange={handleChange}
/>
while for form submission i have used formik and code is shown below:而对于表单提交,我使用了 formik,代码如下所示:
const initialValues = {
organ: "",
env: "",
gcloud_keyfile: "" ,
};
const { values, errors, touched, handleBlur, handleChange, handleSubmit } =
useFormik({
initialValues,
validationSchema: GcpSchema,
onSubmit: async (values, action) => {
try {
let response = await axios.post(
`${state.baseUrl}accounts/gcp/`,
{
organization: values.organ,
environment: values.env,
gcloud_keyfile_json: JSON.stringify(values.gcloud_keyfile),
});
if (response.status === 200) {
setMsg("You Are Successfully Create the Account");
action.resetForm();
return Promise.resolve();
}
} catch (error) {
console.error("There was an error!", error);
}
},
});
i have used json stringify but that doesn't work我使用了 json stringify 但这不起作用
i used JSON.parse(gcloud_keyfile_json) to convert string into object and apply validation on the input field to take only object value我使用 JSON.parse(gcloud_keyfile_json) 将字符串转换为 object 并对输入字段应用验证以仅采用 object 值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.