0

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

1 Answer 1

1

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
})
Sign up to request clarification or add additional context in comments.

3 Comments

No, I am getting the same.
@Mark: What happens if you take out the headers option entirely? What does the class CL_CASE look like? When you debug in the browser, what are the details of the actual HTTP request being made?
Can someone, please explain why it works with no headers?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.