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

212
Vistas
Miagration Asp.Net Core 2 to .Net 6 gave me XMLHttpRequest problems

I folks, I just migrated my ASP.Net Core 2 MVC app to .Net 6, but since that, I have a weird problem: my XMLHttpRequest responses texts are always empty, "{}" or [{},{},{},{}] for arrays, despite my backend really returning data.

Here's an example of a controler method (TestLoad) returning a simple class (TestClass). Notice that when I break on the return line, the value returned is ok, or at least I don't see anything wrong (see image for debug infos): backend

public class TestClass
{
   public int id = 0;
   public string title = "Title";
   public bool active = false;
}

public JsonResult TestLoad()
{
   TestClass testClass = new TestClass();
   testClass.id = 10;
   testClass.title = "Yeah man!";
   testClass.active = true;

   JsonResult jsonRes = Json(testClass);
   return jsonRes;
}

But once on the front end, I got an empty object, not undefined nor null, but really an empty object (see image for debug infos): frontend

var xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200) {
            var dt = JSON.parse(xmlhttp.responseText);
            if (dt == 'err') {   
                alert('error');
            }
            else if (dt !== null) {                    
                alert(dt.title);                    
            }                
        }
        else {
            alert(xmlhttp.status);
        }
    }
}

ldwait(false, false);
xmlhttp.open("GET", rv + "ajxGame/TestLoad", true);
xmlhttp.setRequestHeader('Cache-Control', 'no-store');

xmlhttp.setRequestHeader("Content-Type", "application/json");

xmlhttp.send();

Any help would be greatly appreciated since I completely clueless of what happened. Again, my code hasn't changed, but my project has migrated from .Net Core 2 to .Net 6.

Thank you

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

0

Are you using Razor pages in your Asp.net core 6 ?

If so, the way you call your method maybe the issue. For post requests, call from the client ?handler=TestLoad and on the server make sure the method name is OnPostTestLoad().

Get requests are disabled by default and need to be enabled. Microsoft Warning Message

For more info, check this link from Microsoft docs


Another issue maybe the way you return your class. Try returning it as a JSON object instead.

return new JsonResult(new {objClass: testClass});

And on the client side, get your class as an object property

let objClass = result.objClass;
about 4 years ago · Juan Pablo Isaza Denunciar

0

i would add a response type, and replace onreadystatechange with onload

    xmlhttp.responseType = 'json';

    xmlhttp.onload = () => {
        console.log("load - "+ JSON.stringify(xhr.response));
        var data = xhr.response;
       
    }
about 4 years ago · Juan Pablo Isaza Denunciar

0

Actually, my error was simply that I forgot to add { get; set; } on each property of my TestClass in the backend part! It worked without it in .Net Core 2, but it seems like this is now mandatory in .Net 6

public class TestClass
{
   public int id {get; set;} = 0;
   public string title {get; set;} = "Title";
   public bool active {get; set;} = false;
}

...thanks to those who attempted an answer!

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