I have the react snippet:
const model = {
Code: 'somecode',
Elements: elements,
...more
}
apiPost(url, false, model)
.then(
)
Then:
export function apiPost(url, addAuth, json) {
return apiFetch('post', url, addAuth, json)
}
function apiFetch(method, url, addAuth, json, file) {
let opts = {
method: method,
headers: {}
}
if (addAuth) {
opts.headers = { ...opts.headers, ...authHeader() }
}
if (json) {
opts.headers = { ...opts.headers, ...{ 'content-type': 'application/json' } }
opts.body = JSON.stringify(json);
}
if (file) {
opts.body = file;
}
return fetch(baseUrl() + url, opts).then(handleResponse)
}
I am receiving it at the api side:
[HttpPost]
public async Task<JsonResult> Post([System.Web.Http.FromBody] SurveyEntry model)
{
try
{
...
}
catch (Exception ex)
{
...
}
}
but the model
comes null.
I am puzzled why this happens.
Any suggestions what I am doing wrong?
question from:
https://stackoverflow.com/questions/65644738/i-have-trouble-posting-json-object-from-javascript 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…