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

420
Visualizações
HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

I'm trying to make a post with javascript using ajax to a spring controller


    $("#crear").click(function () {
        var user = document.getElementById("user").value;
        var client = document.getElementById("client").value;
        var documents = document.getElementById("documents").value;
        if (user==null||client==null||documents==null){
            document.getElementById("error").innerHTML='FALTAN COSASsssssssssss';
            alert('Rellene todo los campos!')
        }
        const data={
            fecha_inicio:'A',
            id:'A',
            fecha_entrega:'A',
            usuario:{
                nombre:user
            },
            cliente: {
                nombre:client
            }
        }
        $.ajax({
            url: urlCrearAlta,
            type: "POST",
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            data: data,
            success: function(data) {
                // ...
            }

        });
        $.post(urlCrearAlta,JSON.stringify(data),function (data,satus){
            console.log('${data} and status is ${status}')
        });
        document.getElementById("error").innerHTML=user+client+documents;
    });

and java code

@RequestMapping("/altas")
@RestController
public class AltaRestController {

    private AltaController altaController;

    public AltaRestController(AltaController altaController) {
        this.altaController = altaController;
    }
@PostMapping("/create-alta")
    public void createAlta(@RequestBody AltaDTO altaDTO) {
        altaController.createAlta(altaDTO);
    }

I'm getting the error Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported]

When i use $.ajax it does not send any request, just when i use $.post it send the request

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

0

The problem seems to be how you are using the $.post method. jQuery uses the application/x-www-form-urlencoded;charset=UTF-8 contentType on post by default if you don't specify another contentType. Combined with the @RequestBody annotation at backend (which requires application/json by default) it results in the seen exception and 415 status code.

The solution to this problem should be fairly easy, just add the correct configuration for the .post method, something like this:

$.post({
        url: urlCrearAlta,
        type: "POST",
        contentType: 'application/json',
        dataType: 'json',
        data: JSON.stringify(data)
    })
    .done((data, status) => {
          console.log(data, status);
          //do whatever you like with data and status
    });

Also, as stated in jQuery documentation here, $.post is just a shorthand method for $.ajax, so if $.ajax doesn't work and $.post does, there should be something wrong in how you configure the request or how you handle the callbacks methods. The major suspect for me would be the success function you specify in the $.ajax request, check there is nothing wrong in there, or just use the other callback methods, like .done, .fail and .always

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