0

I have a simple controller:

[HttpPost("/api/LogError")]
public void LogError(string msg)
{
}

and javascript:

    fetch('/api/LogError', {
        method: 'POST',
        body: JSON.stringify({
            'msg': tx
        })
    })

this doesn't work. How should I properly pass a parameter?

2
  • 1
    Your fetch request isn't posting a string. It's posting an object that contains a property of type string. Have you considered changing your action method signature to accept an object that has a property named msg of type string? Commented May 1, 2020 at 17:14
  • thanks mason, but I don't want to change the method signature. Any other way? Commented May 2, 2020 at 13:57

1 Answer 1

1

you need to add data and content type like this:

dataType: 'json',
contentType: 'application/json; charset=utf-8'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.