【发布时间】:2021-11-29 09:55:48
【问题描述】:
我正在尝试创建我的第一个 Web API,我用 C# 编写后端,用 react js 编写前端。
当我尝试通过站点在数据库中添加新人时,我收到错误[object Object]。
但是当我尝试通过 Postman 在数据库中添加新人时,我收到了“添加成功”的消息。
这是我的 js 文件
import React,{Component} from 'react';
import {Modal,Button, Row, Col, Form} from 'react-bootstrap';
export class AddDepModal extends Component{
constructor(props){
super(props);
this.handleSubmit=this.handleSubmit.bind(this);
}
handleSubmit(event){
event.preventDefault();
fetch(process.env.REACT_APP_API+'department',{
method:'POST',
headers:{
'Accept':'application/json',
'Content-Type':'application/json'
},
body:JSON.stringify({
borzhnuka_id:null,
borzh_name:event.target.borzh_name.value
})
})
.then(res=>res.json())
.then((result)=>{
alert(result);
},
(error)=>{
alert('Failed');
})
}
render(){
return (
<div className="container">
<Modal
{...this.props}
size="lg"
aria-labelledby="contained-modal-title-vcenter"
centered
>
<Modal.Header clooseButton>
<Modal.Title id="contained-modal-title-vcenter">
Додати
</Modal.Title>
</Modal.Header>
<Modal.Body>
<Row>
<Col sm={6}>
<Form onSubmit={this.handleSubmit}>
<Form.Group controlId="borzh_name">
<Form.Label>Ім'я</Form.Label>
<Form.Control type="text" name="borzh_name" required
placeholder="borzh_name"/>
</Form.Group>
<Form.Group controlId="borzh_last_name">
<Form.Label>Прізвище</Form.Label>
<Form.Control type="text" name="borzh_last_name" required
placeholder="borzh_last_name"/>
</Form.Group>
<Form.Group controlId="amount">
<Form.Label>Amount</Form.Label>
<Form.Control type="int" name="amount" required
placeholder="amount"/>
</Form.Group>
<Form.Group controlId="Date_of_offer">
<Form.Label>Дата приєднання</Form.Label>
<Form.Control
type="date"
name="Date_of_offer"
required
placeholder="Date_of_offer"
/>
</Form.Group>
<Form.Group>
<Button variant="primary" type="submit">
Додати
</Button>
</Form.Group>
</Form>
</Col>
</Row>
</Modal.Body>
<Modal.Footer>
<Button variant="danger" onClick={this.props.onHide}>Close</Button>
</Modal.Footer>
</Modal>
</div>
)
}
}
这是 C# 文件中的 POST 处理程序:
[HttpPost]
public JsonResult Post(Department dep)
{
string query = @"insert into dbo.All_borzh values
('" + dep.borzhnuka_id + @"',
'" + dep.borzh_name + @"',
'" + dep.borzh_last_name + @"',
'" + dep.amount + @"',
'" + dep.Date_of_offer + @"')
";
DataTable table = new DataTable();
string sqlDataSource = _configuration.GetConnectionString("EmployeeAppCon");
SqlDataReader myReader;
using (SqlConnection myCon = new SqlConnection(sqlDataSource))
{
myCon.Open();
using (SqlCommand myCommand = new SqlCommand(query, myCon))
{
myReader = myCommand.ExecuteReader();
table.Load(myReader); ;
myReader.Close();
myCon.Close();
}
}
return new JsonResult("Added successfully");
}
【问题讨论】:
-
SQL Injection alert - 你应该永远将你的 SQL 语句连接在一起 - 使用 参数化查询 来避免 SQL注入 - 查看Little Bobby Tables
-
另外——对于
INSERT,你不应该使用.ExecuteReader()方法——毕竟,你不是读取从SQL Server返回的任何数据——你重新添加新数据。使用.ExecuteNonQuery(),然后你可以删除它周围所有不需要的东西(阅读器、数据表等)..... -
你能举例“使用 .ExecuteNonQuery() 然后你可以把它周围的所有东西都扔掉”
标签: api asp.net-core-webapi webapi