I have this controller in .net
[HttpPost]
[Route("check-code")]
public ActionResult CheckDiscountCode(string code)
{
string DiscountedCode = "MYCODE";
if ( DiscountedCode == code.ToString().ToUpper() ) {
return Json(true);
}
else
{
return Json(false);
}
}
In React I have an axios request like this :
const response = await axios.post(process.env.REACT_APP_API_URL + '/check-code', discountCode)
Basicaly and I expect it to return if what the user has put in is valid or not. But when I breakpoint it everytime it says that the code
is null and returns false no matter what I put in…
2
Answers
Accordingly to axios documentation, you need to specify the body of the request in the second argument of the post function, so make sure the discountCode passed in the second parameter is a object containing the field
code
and also make sure that in your controller you’re expecting a body to read the code field from.If post as
axios.post(url,discountCode)
,your payload will be like this:
solution 1:
solution 2: (change param name)