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

251
Visualizações
The .net core mvc is reaching the json controller I POST, but I can't read it. I also get error code 415 when I use [FromBody]

I'm posting a data with XHR in the project I'm coding. A JSON reaches the Controller but if I use [FromBody] I get error code 415. If I don't use it, the incoming JSON looks empty.

Controller

[HttpPost]
[IgnoreAntiforgeryToken]
[Route("revizedCategoryzfc")]
public void revizedCategoryzfc([FromBody] Category model) 
{
    if (model.fileCode != null)
    {
        System.Console.WriteLine(model.fileCode);
    }
    System.Console.WriteLine("Girdi");
}

Post XHR Code

async function makeRequest(method, url, model) {
    return new Promise(function(resolve, reject) {
        let xhr = new XMLHttpRequest();
        xhr.open(method, url);

        xhr.onload = function() {
            if (this.status >= 200 && this.status < 300) {
                resolve(xhr.response);
            }else {
                reject({
                    status: this.status,
                    statusText: xhr.statusText
                });
            }
        };

        xhr.onerror = function() {
            reject({
                status: this.status,
                statusText: xhr.statusText
            });
        };

        xhr.send(model);
    });
}

document.querySelector('.save-service').addEventListener('click', async () => {
    for (var es of document.querySelectorAll('.category-item'))
    {
        await makeRequest('POST', '/zfc/revizedCategoryzfc', JSON.stringify({
            "Name": es.querySelector('.service-name').value,
            "fileCode": es.querySelector('.filecodas').value,
            "CategoryId": es.querySelector('.zfc-id').value
        }))
    }
})

Error

enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Since you user json strngify and frombody, you have to add this header to your xhr

xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
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