Home > Mobile >  Axios request to .net controller gives value of none
Axios request to .net controller gives value of none

Time:06-23

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...

CodePudding user response:

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.

CodePudding user response:

If post as axios.post(url,discountCode), your payload will be like this:

{
    discountCode : ""
}

solution 1:

await axios.post(url, {code:discountCode})

solution 2: (change param name)

public ActionResult CheckDiscountCode(string discountCode) 
  • Related