Dr *_*car 5 javascript c# reactjs asp.net-core axios
使用 axios.post 将一个简单的值 int 发送到我在 ASP.NET Core 上的控制器,当发送任何值时,方法控制器接收值“0”。
使用 axios 发送此类值的正确方法是什么(发布或删除)?
PD:我可以使用 [FromBody] 正确发送模型并在控制器上接收
方法控制器:
[Route("Delete"),HttpPost]
public async Task<ActionResult> Delete(int id)
{
try{
var result = await userService.DeleteUserPerson(id);
return Json(new{
response=true,
data=result,
error=""
});
}
catch(Exception ex){
return Json(new{
response=false,
data=false,
error=ex.Message
});
}
}
Run Code Online (Sandbox Code Playgroud)
反应类中的方法:
async function DeleteUser(id, props){
var request= new Request({});
try{
console.log(id);
var axiosResp= await request.axios_request.post('User/Delete', JSON.stringify({id:id}));
if(axiosResp.status!=200){
//do smething
}
//this case validate error
if(axiosResp.data.response && !axiosResp.data.error){
//do something
}
//do something
}catch(err){
//do something
}
}
Run Code Online (Sandbox Code Playgroud)
类请求(axios):
export default class Request {
constructor(){
this.axios_request = axios.create({
baseURL: 'http://localhost:5000/api',
timeout: 5000,
headers: authHeader()
});
}
}
Run Code Online (Sandbox Code Playgroud)
测试不同的方法,这对我有用:
[Route("Delete/{id}"),HttpDelete("{id}")]
public async Task<ActionResult> Delete([FromRoute]int id){}
Run Code Online (Sandbox Code Playgroud)
在 axios 上使用删除:
request.axios_request.post('User/Delete/'+ id);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18856 次 |
| 最近记录: |