Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

I have trouble posting json object from javascript

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...