Javascript 控制器方法赢得';我没有参数
我向服务器端发出了一个post ajax请求。 请求已到达服务器,但收到的参数为空/null 不知道为什么会发生这种情况,问题可能出在服务器上。尝试了许多解决方案,但没有任何改变 我希望你们中的一些人能帮助我Javascript 控制器方法赢得';我没有参数,javascript,ajax,.net-core,fetch,Javascript,Ajax,.net Core,Fetch,我向服务器端发出了一个post ajax请求。 请求已到达服务器,但收到的参数为空/null 不知道为什么会发生这种情况,问题可能出在服务器上。尝试了许多解决方案,但没有任何改变 我希望你们中的一些人能帮助我 fetch('Home/AddMovie', { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'a
fetch('Home/AddMovie', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
movie: data_object
}),
}).then(res => res.text())
.then(text => {
})
.catch((error) => {
console.error('Error:', error);
});
[HttpPost]
公共电影(电影)
{
var movies=新列表();
var newJson=“”;
使用(StreamReader r=newstreamreader(JsonFilePath))
{
字符串json=r.ReadToEnd();
movies=JsonConvert.DeserializeObject(json);
添加(电影);
newJson=JsonConvert.Serialized对象(电影、格式化、缩进);
}
System.IO.File.WriteAllText(JsonFilePath,newJson);
}
这会解决的
public void AddMovie([FromBody]Movie Movie){
编辑:
删除JSON.stringify()只需在删除JSON.Stringfy-->正文{data_object}后分配对象本身。控制器动作中的movie参数为null。
[HttpPost]
public void AddMovie(Movie movie)
{
var movies = new List<Movie>();
var newJson = "";
using (StreamReader r = new StreamReader(JsonFilePath))
{
string json = r.ReadToEnd();
movies = JsonConvert.DeserializeObject<List<Movie>>(json);
movies.Add(movie);
newJson = JsonConvert.SerializeObject(movies, Formatting.Indented);
}
System.IO.File.WriteAllText(JsonFilePath, newJson);
}