I am reading data from the mysql database and i am saving them into an array which looks like this:
Array
(
[0] => aargau
[1] => appenzell_au
[2] => appenzell_in
[3] => basel-land
[4] => basel-stadt
[5] => bern
[6] => freiburg
[7] => genf
[8] => glarus
[9] => graub�nden
[10] => jura
[11] => luzern
[12] => neuenburg
[13] => nidwalden
[14] => obwalden
[15] => obwalden
[16] => schwyz
[17] => solothurn
[18] => st_gallen
[19] => tessin
[20] => thurgau
[21] => uri
[22] => waadt
[23] => wallis
[24] => zug
[25] => z�rich
)
THE CODE (client.class.php) THAT DOES THIS LOOKS LIKE THIS :
public function getCantons($country){
$cantonsArray= array();
// $tmpArray = array();
$sql_get_cantons = sprintf("SELECT name FROM locations WHERE country='".$country."'");
$result = $this->CONNECTION->query($sql_get_cantons);
while($row = mysqli_fetch_array($result)){
array_push($cantonsArray,$row['name']);
}
return $cantonsArray;
}
I HAVE AN OTHER PHP FILE (server.php) which returns the answer to the ajax call
if($_POST['command']=='cantons'){
$country = $_POST['country'];
$client = new Client();
// $results = $client->getActiveItemsOfCategory('technology');
$results = $client->getCantons($country);
$jsonResults = json_encode($results);
print_r($jsonResults);
}
IN THE AJAX CALL (which is working properly becauseI use I use the same code as in other AJAX calls) I simply do this :
var jqxhr = $.post( "php/server.php", {country:COUNTRY,command:'cantons'},function() {
})
.done(function(data) {
console.log(data);
})
.fail(function(data) {
alert(JSON.stringify(data));
})
.always(function() {
});
THE result is empty
Try adding this to your server.php
header('Content-Type: application/json');
echo $jsonResults;
Use echo
instead of print_r
to output your JSON string
The problem was the encoding of the letters. Apparently, I am using German alphabet that was the reason for this error.