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

333
Visualizações
$.ajax() returns syntax error: Unexpected end of json input

So I'm working on a website where I have to implement a chat, currently the whole thing is running on localhost. I'm getting this error:

SyntaxError: Unexpected end of JSON input

and can't figure out why. I have googled a little but can't find an answer, that actually works. I actually did this yesterday, on another computer and that worked super, but today it won't work and I can't figure out why.

Thank you for the great answers.

$(function() {
    updateChat("updateChat", null);

    $(".chat-form").submit(function(event) {
        event.preventDefault();
        if ($(".chat-form input").val() != "") {
            updateChat("sendMessage", $(".chat-form input").val());
        }
    });

    setInterval(function() {
        updateChat("updateChat", null);
    }, 3000);

    function updateChat(method, message) {
        $.ajax({
            type: "POST",
            url: "action/chat.php",
            data: {
                function: method,
                message: message
            },
            dataType: "json",
            success: function(data) {
                console.log(data);
            },
            error: function (request, status, error) {
                console.log(error);
            }
        })
    }
})
about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Most likely there's an error or warning in your PHP code being displayed, and because you are expecting only json, that causes the syntax error.

There are a few ways to find out what's going on:

  • open the developer console in your browser and see what the response is the network tab
  • check your PHP error log
  • temporarily change your dataType to html and you'll see your console.log(data)
about 4 years ago · Santiago Trujillo Relatório

0

I was getting this error due to my backend php function NOT returning a response as it should have been. It was returning nothing. My parent function that should have been returning the response was calling a child function that WAS returning a response, but the parent function wasn't passing that child return back to the ajax call.

about 4 years ago · Santiago Trujillo Relatório

0

Another possible culprit for these type of errors could be an improper python "shebang" on your back-end (server side) script.

In my particular case I had ajax call to python cgi script via Apache web server and I could not find a more descriptive error message at front-end debug tools. However, Apache logs indicated that the back-end script had problems importing a one of the python scripts because the interpreter did not recognize it. After checking the "shebang" of that back-end script sure enough it had the wrong interpreter specified because I just copied a template file over and forgot to modify it..

So, check your "shebang" at the top of your script to make sure it points to correct interpreter. Example: enter image description here

about 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