Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

206
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda