c# - 需要帮助解决 400 错误请求
问题描述
我试图将给定数据获取到服务器中的 POST 方法 getNumbers,它有一个参数 id,它只是返回 id 用于调试目的。所以在客户端我做如下:
documentClickHandler=(number)=> {
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ id: number })
};
fetch("http://localhost:5000/api/Documents/GetNumber", requestOptions)
.then(response => response.json())
.then(result => console.log(result))
.catch(error => { console.log('request failed', error); });
}
Which only has the purpuse to give the id to the getNumber method in the server:
[HttpPost("GetNumber")]
public string GetNumber([FromBody] string id)
return id;
}
This gives me an error (400 bad request) and I dont know how to solve it. Any ideas? Im in desperate need of help.
解决方案
从您的代码中删除单词“数字”。这是一个保留字
试试这个代码:
documentClickHandler=(documentId)=> {
var id="123"; // try this value at first, after this use your value
const requestOptions = {
method: 'POST',
body: {id: id}
};
fetch("http://localhost:5000/api/Documents/GetNumber", requestOptions)
.then(response => response.json())
.then(result => console.log(result))
.catch(error => { console.log('request failed', error); });
}
和行动
[Route("~/api/Documents/GetNumber")]
public string GetNumber( string id)
return id;
}
推荐阅读
- reactjs - 在 React/Firebase 上更改用户的授权
- python - 如何修复我的音乐测验排行榜
- flutter - 构建颤振 apk
- python - 在 Python 中的函数内用作变量名一部分的参数
- ansible - Ansible - 动态目录结构创建
- java - 使用 Java 和 Apache POI 使用地图的结果填充 xlsx 文件的空单元格
- android - 单击添加按钮时,如何在现有卡片视图下方添加卡片视图?
- java - 多线程并行查询中的 ResultSetHandler 问题
- networking - 只允许特定国家连接服务器(谷歌云平台)
- vue.js - 从模板访问 vuex 状态数据