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

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

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