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

322
Vistas
jQuery ajax POST, error 'SyntaxError: Unexpected end of JSON input'

Im sure im missing something simple, but so far struggling to see what...

I am wanting to POST some form field entries to a server in JSON format. I am currently using:

$(document).ready(function () {
    $("#newEnquiery").submit(function (event) {     
        event.preventDefault(); 
        $.ajax({
            url: 'SERVER-URL-HERE',
            type: 'POST',
            data: JSON.stringify({ "name": $('#name').val(), "emailAddress" : $('#emailAddress').val(), "address" : $('#address').val() }),
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function(data, textStatus, jQxhr){
                console.log('Sent');
            },
            error: function( jqXhr, textStatus, errorThrown ){
                console.log('fail');
                console.log(errorThrown);
            }
        });
    });
});

The error that I am being shows in the console is; SyntaxError: Unexpected end of JSON input

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

$.ajax({
    url: "https://jsonplaceholder.typicode.com/posts",
    type: "POST",
    data: JSON.stringify({
        name: "test",
        emailAddress: "test",
        address: "test",
    }),
    contentType: "application/json",
    dataType: "json",
    success: function (data, textStatus, jQxhr) {
        console.log(data);
    },
    error: function (jqXhr, textStatus, errorThrown) {
        console.log("fail");
        console.log(errorThrown);
    },
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

contentType has to be application/json and you should pass a plain json into the JSON.stringify method into the data attribute. So the code has to be:

    $(document).ready(function () {
    $("#newEnquiery").submit(function (event) {     
        event.preventDefault(); 
        $.ajax({
            url: 'SERVER-URL-HERE',
            type: 'POST',
            data: JSON.stringify({ name: $('#name').val(), emailAddress : $('#emailAddress').val(), address : $('#address').val() }),
            contentType: 'application/json',
            dataType: 'json',
            success: function(data, textStatus, jQxhr){
                console.log('Sent');
            },
            error: function( jqXhr, textStatus, errorThrown ){
                console.log('fail');
                console.log(errorThrown);
            }
        });
    });
});
over 4 years ago · Santiago Trujillo 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