I'm using Vue.js and I'm trying to make post request and send data { name: 'TestName' } to controller, but when I hit controller, instead name to be equal to "TestName" it's null.
This is my Fetch: post request
`sendName() {
let url = 'https://localhost:44362/Home/ReceiveName'
fetch(url, {
method: 'POST',
body: JSON.stringify({ name: 'TestName' })
}).then(function (response) {
response
}).catch(err => {
err
});
}`
And this is my action method ReceiveName in the HomeController
`[HttpPost]
public string ReceiveName(string name)
{
*some code*
}`
Here name must be "TestName", but it's NULL
I had tried answers from this question How to pass data to controller using Fetch api in asp.net core , to set [FromBody] in ReceiveName method like this public string ReceiveName( [FromBody] string name), but it doesn't worked for me. Then I tried to add headers to fetch body
`headers: {
'Content-Type': 'application/json'
},`
But when i do this i get that error in browser
Failed to load https://localhost:44362/Home/ReceiveName: Response for preflight does not have HTTP ok status.
I think it's because CORS rules.
I'm running my backend on https://localhost:44362 and my frontend on http://localhost:8080/ so I had to edit my CORS rules in web.config like this
`<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>`
My question is - How can I receive my data from Fetch:post to my action method?
Is there a need to make query string?
But I don't want to send my string "TestName" as FormData I know that if I send it like that I will receive it in my action method, but is this is a good way to send a string?