Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

101
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!