我只是好奇我应该如何在 superagent 中使用删除,很难搜索任何删除 superagent 文档我目前使用 asp.net 作为我的 API,reactjs 作为前端我有这个按钮:
<td><center><button className ="btn btn-danger" onClick={this.deleteEmployee.bind(this, d.Employee_ID)}>Delete</button></center></td>
如何使用 fetch 将其连接到 superagent 上? 我正在使用本地主机:
http://localhost:5118/api/employeedetails/DeleteEmployeeDetail/
使用
del()
请参阅 文档
DELETE 也可以称为 .del() 以兼容旧版 IE 其中删除是保留字。
npm install superagent
var request = require('superagent');
deleteEmployee(id) {
request
.del('/api/employeedetails/DeleteEmployeeDetail')
.send({ id: id })
.set('Accept', 'application/json')
.end(function(err, res){});
}
如果直接使用删除请求,可能会收到405错误。
例如,您可能有一个像这样的端点
Route::middleware('auth:api')->delete('/orders/{id}', 'ApiController@destroyOrder');
然后您使用超级代理发出删除请求,如下所示
superagent
.del(`${process.env.BASE_URL}/api/orders/${params.id}`)
.send(body)
.set('Content-Type', 'application/json')
.set('Authorization', `Bearer ${process.env.API_TOKEN_SECRET}`)
.then(response => {})
.catch(error => {})
并得到如下响应:“此路由不支持 GET 方法”
为了解决这个问题,这里有一个替代方案:
superagent
.post(`${process.env.BASE_URL}/api/orders/${params.id}`)
.send({ _method: "DELETE" })
.set('Content-Type', 'application/json')
.set('Authorization', `Bearer ${process.env.API_TOKEN_SECRET}`)
.then(response => {})
.catch(error => {})