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

422
Vistas
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 Respuestas
Responde la pregunta

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 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