Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

96
Vistas
json string on POST to MVC action method using AJAX is null

I am getting a hard time to find out why the string sent via AJAX request is null. Console.WriteLine(data) shows empty. Status is 200 OK. If I add some code to parse the string received, I get an error stating that JObject.Parse cannot be null. I don't know what am I missing. The javascript code is ok. The action method also seems ok, but my knowledge on Asp.Net Core and MVC is very scarce, so I am not sure. Can someone please point out what am I missing?

The javascript code:


    let obj = {email: email_address.value};
    let objStringified = JSON.stringify(obj);
    $.ajax({
       type: 'POST',
       contentType: 'application/json; charset=UTF-8',
       data: objStringified,
       url: '@Url.Action("ReturnCheckAccountDuplication")',
       dataType: 'text',
       success: function(data) {
          console.log(data);
       },
       error: function(error) {
          console.log("Keep trying", error);
       }
    });

C# code:

[HttpPost]
public ActionResult ReturnCheckAccountDuplication([FromBody] string data)
{
   Console.WriteLine(data);
   JObject jObject = JObject.Parse(data);
   string email = (string)jObject["email"];
   bool emailExists = CheckAccountDuplication.Get(email);
   string returnResult = emailExists.ToString();
   return Content(returnResult);
}

The solution on the controller side is

public class AccountCheckModel
{
   public string Email { get; set; }
}

[HttpPost]
public ActionResult ReturnCheckAccountDuplication([FromBody] AccountCheckModel data)
{
   string result = CheckAccountDuplication.Get(data.Email).ToString();
   return Content(result);
}

Thanks to all the members who commented on my problem, especially John Glenn who provided a solution. I had been trying for several days but without success. My knowledge of Asp.Net Core is very poor indeed. Thank you very much.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The easiest solution is to create a model representing the JSON data that your controller will receive. For example, create a class like so:

public class AccountCheckModel
{
    public string email { get; set }
}

Then, use it as the parameter for your controller method:

public ActionResult ReturnCheckAccountDuplication([FromBody] AccountCheckModel data)

This is the preferred way to access the request body. To get the request body as a string, you have to jump through some serious hoops.

about 4 years ago · Juan Pablo Isaza Denunciar

0

An alternative way to send your data via AJAX to your Controller:

var json = {
       email: email_address.value
};

$.ajax({
   type: 'POST',
   data: {'json': JSON.stringify(json)},
   url: '@Url.Action("ReturnCheckAccountDuplication")',
   dataType: 'json',
   success: function(data) {
      console.log(data);
   },
   error: function(error) {
      console.log("Keep trying", error);
   }
});

And your Controller:

[HttpPost]
public ActionResult ReturnCheckAccountDuplication(string json)
{
   Console.WriteLine(json);
   JObject jObject = JObject.Parse(json);
   string email = (string)jObject["email"];
   bool emailExists = CheckAccountDuplication.Get(email);
   string returnResult = emailExists.ToString();
   return Content(returnResult);
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda