【发布时间】:2021-09-10 17:15:17
【问题描述】:
我试图获取一个给定数据到服务器中的 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.
【问题讨论】:
-
您可以使用浏览器中的调试工具查看发送到服务器的请求吗?
-
@AliK id 没有发送到服务器
-
看来你没有在任何地方传递 id 你只是声明它 id 将使用 {id:'abc'} 之类的东西传递所以试试这个而不是 id:number。
-
@AliK 我认为问题在于服务器从不从客户端接收 id。当客户端单击一个表时,它会控制台记录 id,但服务器从不接收该 id,我不明白
-
您是否在控制器构造函数上放置了断点,以确保在您开始调用时会命中控制器?
标签: c# asp.net asp.net-mvc asp.net-core .net-core