Mark
Mark

Reputation: 4970

Angular POST and Web API

My code is as the following:

    var response = $http({
        method: 'post',
        withCredentials: true,
        dataType: "json",
        data: JSON.stringify(payload),

        headers: {
           'Content-Type': 'application/json; charset=utf-8',
        },
        url: url
    });

where payload looks like this: {"CASEID":3,"CASENUMBER":"ANY ","TITLE":"ANY "}

Backend code:

public CL_CASE Post([FromBody]CL_CASE value)
    {....

When running it as it's shown value is null. If I change headers to 'Content-Type': 'application/x-www-form-urlencoded' then I do get value but with properties equal to null/0 . What am I doing wrong?

Thanks

Upvotes: 0

Views: 51

Answers (1)

David
David

Reputation: 219047

You don't need to call JSON.stringify. This results in sending a string to the server, not an object. And since the WebAPI model binder is expecting a CL_CASE object, it has no way to populate that object from just a string.

Simply send the object itself:

data: payload

To be honest, I don't think you need the headers option at all in this case either. Let the default functionality handle it:

$http({
    method: 'post',
    withCredentials: true,
    dataType: 'json',
    data: payload,
    url: url
})

Upvotes: 1

Related Questions