javascript - 如何更改 FormData() 的值以接收文本框的值?
问题描述
我正在从 API 获取数据并将其放入表中,到目前为止反应非常好,但是当我尝试从表中的一行编辑数据时。这给了我错误“PUT 400(错误请求)”。我应该如何更改 FormData() 以从“<Form.Control/>”接收值,然后从后端接收它?
---Editar2.js---
handlerSubmit = async (event) => {
event.preventDefault();
let formData = new FormData();
formData.set("", event.target.legenda.value);
let resposta = await fetch("api/FotografiasAPI/" + this.props.idLinha, {
method: "PUT",
headers:{'Accept':'application/json','Content-Type':'application/json'},
body: formData
});
if (!resposta.ok) {
console.error("error:" + resposta.status);
}
return await resposta.json();
}
render(){
return(
<Modal
{...this.props}>
<Form onSubmit={this.handlerSubmit}>
<Form.Group controlId="legenda">
<Form.Label>legenda</Form.Label>
<Form.Control defaultValue = {this.props.legenda} type="text"name="legenda" required placeholder="legenda"/>
</Form.Group>
<Form.Group>
<Button variant="primary" type="submit">
Editar Restaurante
</Button>
</Form.Group>
</Form>
</Modal>
);}}
---后端---
[HttpPut("{id}")]
public async Task<IActionResult> PutFotografias(int id, [FromForm] Fotografias fotografias)
{
if (id != fotografias.ID)
{return BadRequest();}
_context.Entry(fotografias).State = EntityState.Modified;
try
{await _context.SaveChangesAsync();}
catch (DbUpdateConcurrencyException)
{
if (!FotografiasExists(id))
{return NotFound();}
else
{throw;}}
return NoContent();
}
解决方案
推荐阅读
- javascript - if/else 逻辑 - React Native
- ruby-on-rails - Net::SMTPAuthenticationError (504 5.3.3 AUTH 机制 PLAIN 不可用
- javascript - 如何更改 datetimepicker xdsoft 日历的字体大小
- postgresql - Postgres 基于唯一约束的列值
- javascript - 即使在调整大小后如何在 flexbox 容器中排列项目?
- azure - clientCredentialType="Certificate" 后出错
- c++ - C++:编译时检查匹配的函数调用对?
- django - TypeError:as_view() 采用 1 个位置参数,但 2 个在 Django 中给出
- azure - Azure 上的 Visual Studio 6.0 应用
- python - 如何在 Django 模型中定义的模板中调用函数?