I'm trying to make a GET request with ajax and PHP, but I always get the following error:
text status=:parsererror, error thrown:=SyntaxError: Unexpected token < in JSON at position 0
My ajax request is as follows:
$.ajax({
url: 'private/ajax/get_personas.php',
type:'GET',
contentType: 'application/json; charset=utf-8',
dataType:'JSON',
success: function(data, textStatus, jqXHR) {
alert(data);
},
error: function(data, textStatus, errorThrown) {
console.log('message=:' + data + ', text status=:' + textStatus + ', error thrown:=' + errorThrown);
}
});
The file get_personas.php contains only the following lines of code:
include('private/connection.php');
echo json_encode("hello");
And the connection.php file
$host = "localhost";
$user = "root";
$pass = "12345";
$db = "pruebas";
$port = 3306;
$connection = mysqli_connect($host, $user, $pass, $db, $port);
The strange thing is that if I include (I copy all the code) the lines of code in the connection.php file in the file get_personas.php everything works correctly. I have tried to solve the problem but I have not achieved it, I hope someone can help me. I do not want to have to add all the connection code in each ajax request that you make.