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

184
Visualizações
jQuery $.post needs JSON response parsed manually all of a sudden

I'm calling to the server using jQuery's $.post method and assigning the JSON response to a variable called result. The response is an array in PHP's json_encode() function.

Up until now I've always just used the properties of result directly, as the JSON gets parsed into an object automatically. But now, after some debugging, I've found that my code only works when I use JSON.parse(result) first.

What could've caused this sudden change?

Solution: Adding header("Content-Type: application/json") serverside when echoing a JSON encoded response.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Either:

  1. Your server stopped setting the correct Content-Type response header (application/json). PHP will claim anything it sends to the browser is text/html unless you use the header() function to override that.
  2. You didn't say type: 'json' to make jQuery (a) set an Accept header and (more relevantly) (b) ignore the response's Content-Type header and try to parse it as JSON regardless.
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