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

109
Visualizações
How to remove unwanted tags from JSON response

I'm using PHP MVC Model to build a Website, I want to use JSON in Ajax and fetch data from the database, but response always returns HTML tags and texts that I don't want. Here is the Ajax code:

function readData() {
$.ajax({
  url: 'http://example.com/?c=filename',
  type: 'GET',
  success: function (response) {
    console.log(response);
    var parse = JSON.parse(response);
  }
 });
}

 readData();

Here is my PHP model file:

public function select(){
 $select_query = $this->db->prepare("SELECT * FROM table_name");
 $select_query->execute();
 $row = $select_query->fetchAll(PDO::FETCH_ASSOC);
 echo json_encode($row, JSON_UNESCAPED_UNICODE);
}

and here is my PHP controller file:

$dashboard = new obj();  //class that contains `select` function in model file
$dashboard->select();
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In your file.js add 2 args: dataType and contentType.

$.ajax({
  url: 'http://example.com/?c=filename',
  type: 'GET',
  dataType: 'JSON',
  contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
  success: function (response) {
    console.log(response);
    var parse = JSON.parse(response);
  }
 });
  • dataType: 'JSON'
  • contentType: 'application/x-www-form-urlencoded; charset=UTF-8',

In your PHP file add at top this line:

header('Content-Type: application/json; charset=UTF-8');

This line should avoid to catch it like html file. Use POSTMAN to test your request without Javascript. Postman can send a simple GET to

https://YourServerLocalOrVPStester/?c=filename

Perhaps, can use chrome, firefox, operaGx, or whatever you use as browers to make a simple call url too without javascript or Postman

Postman will autodetect headers and show it like JSON automatically. POSTMAN Result viewer

If works then try it from ajax and should works too.

about 4 years ago · Juan Pablo Isaza 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