Javascript POST响应给出错误:“引用;无法将JSON值转换为api.Models.Account“;

Javascript POST响应给出错误:“引用;无法将JSON值转换为api.Models.Account“;,javascript,c#,json,post,controller,Javascript,C#,Json,Post,Controller,在向我的C#.NET后端控制器发送JSON POST请求时,我遇到了一个错误,这给了我一个POST错误代码400,我理解这意味着错误的请求。 我向用户(健身房的健身教练)提示新培训课程的信息,如下所示(为了简洁起见,删除了额外代码): 下面是帐户类,它与会话一样,位于错误中提到的模型名称空间/目录中: class Account { string Username; string Password; string FName; string LName;

在向我的C#.NET后端控制器发送JSON POST请求时,我遇到了一个错误,这给了我一个POST错误代码400,我理解这意味着错误的请求。 我向用户(健身房的健身教练)提示新培训课程的信息,如下所示(为了简洁起见,删除了额外代码):

下面是
帐户
类,它与
会话
一样,位于错误中提到的模型名称空间/目录中:

class Account {
    string Username;
    string Password;
    string FName;
    string LName;
    string Type;
}
my
SessionController的POST方法:

[HttpPost]
public void Post([FromBody] Session session) {
    Console.WriteLine("Post reached. session is " + session.ToString());
}
我知道我的JSON格式不正确,无法放入
会话
对象。我的错误是控制器无法接受我的POST请求发送的JSON:

fetch(api/url, {
    method: "POST",
    headers: {
        "Accept": 'application/json',
        "Content-Type": 'application/json'
    },
    body: JSON.stringify({
        Title : title,
        ExerciseType: category,
        Trainer: JSON.stringify({
            Username: sessionStorage.loginUsername,
            Password: null,
            FName: sessionStorage.loginfName,
            LName: sessionStorage.loginlName,
            Type: sessionStorage.loginType
        }),
        DateTime: datetime
    })
}).catch(function(error, cont..));
我尝试过在某些地方混合使用
JSON.stringify()
,而不是在其他地方,或者不是在所有地方,但我总是会遇到javascript错误——这似乎是我能得到的最远的结果。有人能帮我将我的
帐户
对象嵌套在会话负载中吗


编辑:最后,我真的只需要发送主持课程的培训师的用户名。有更简单的方法吗?

我认为最外层对象上的一个JSON.stringify在fetch(POST)请求体中就足够了

body:JSON.stringify({
标题:标题,,
练习类型:类别,
培训师:{
用户名:sessionStorage.loginUsername,
密码:null,
FName:sessionStorage.loginfName,
LName:sessionStorage.LoginName,
类型:sessionStorage.loginType
},
DateTime:DateTime,
持续时间:持续时间,
容量:容量,
比率:比率
})

我还注意到您的json对象中有一些在.NET模型中不存在的附加字段,如持续时间、容量和速率(我建议您确保.NET模型也捕获这些字段)。

我刚才重试过,没有错误,我的数据显示在数据库中。不知道之前发生了什么,因为我没有改变任何东西。

这样,我得到一个错误:
JSON输入意外结束
fetch(api/url, {
    method: "POST",
    headers: {
        "Accept": 'application/json',
        "Content-Type": 'application/json'
    },
    body: JSON.stringify({
        Title : title,
        ExerciseType: category,
        Trainer: JSON.stringify({
            Username: sessionStorage.loginUsername,
            Password: null,
            FName: sessionStorage.loginfName,
            LName: sessionStorage.loginlName,
            Type: sessionStorage.loginType
        }),
        DateTime: datetime
    })
}).catch(function(error, cont..));