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();
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);
}
});
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.

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