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

110
Vistas
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 Respuestas
Responde la pregunta

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