Home > Back-end >  Vue sends a null parameters in object in axios post request
Vue sends a null parameters in object in axios post request

Time:12-15

So as title says, the problem is that I send a request from Vue using axios which has data on the parameters when I'm sending them (I see it using a console.log), but when I get the request on C# (.NET Core) those parameters are null. If there is more info or code needed just let me know.

Thanks.

Vue:

methods: {
    translate() {
      //This log RETURNS DATA, so parameters aren't null at this moment
      console.log(this.textToTranslate, this.fromLanguage, this.toLanguage);
      axios
        .post(
          "http://localhost:5000/api/Translate",
          {
            TranslateRequest: {
              textToTranslate: this.textToTranslate,
              fromLanguage: this.fromLanguage,
              toLanguage: this.toLanguage,
            },
          },
          {
            headers: {
              "Access-Control-Allow-Origin": "*",
              "Access-Control-Allow-Methods":
                "GET, POST, PATCH, PUT, DELETE, OPTIONS",
              "Access-Control-Allow-Headers":
                "Origin, Content-Type, X-Auth-Token",
            },
          }
        )
        .then((res) => {
          console.log(res);
          this.translatedText = res.data;
        })
        .catch((err) => {
          console.log(err.response);
          alert(err.response);
        });
    },
  },

C#:

    [ApiController]
    [Route("api/[controller]")]
    public class TranslateController : ControllerBase
    {
        [EnableCors("AllowOrigin")]
        [HttpGet]
        public string GetTranslatedText(TranslateRequest request)
        {
            //request object isn't null but their parameters are
            return TranslateText(request);
        }
     }

CodePudding user response:

Your problem is here:

{
    "TranslateRequest":{
        "textToTranslate":"this.textToTranslate",
        "fromLanguage":"this.fromLanguage",
        "toLanguage":"this.toLanguage"
    }
}

Pass data as below:

{
    "textToTranslate":"this.textToTranslate",
    "fromLanguage":"this.fromLanguage",
    "toLanguage":"this.toLanguage"
}
  • Related