P
P
PetuninNikita2014-07-21 16:53:26
JavaScript
PetuninNikita, 2014-07-21 16:53:26

Cyrillic and JSON in Phonegap - how to fix the problem?

Good afternoon dear friends. I ran into a problem in working with JSON on Phonegap. The matter is that I receive the data from the server where they are stored in coding UTF-8.
Server Handler

foreach ($cyr_chars as $cyr_char_key => $cyr_char) { 
$json_str = str_replace($cyr_char_key, $cyr_char, $json_str); 
} 
return $json_str; 
} 

$sql = "select e.id, e.firstName, e.lastName, e.title, e.picture, count(r.id) reportCount " . 
    "from employee e left join employee r on r.managerId = e.id " .
    "group by e.id order by e.lastName, e.firstName";

try {
  $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);	
  $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $stmt = $dbh->query($sql);  
  $employees = $stmt->fetchAll(PDO::FETCH_OBJ);
  $dbh = null;
  echo '{"items":'. json_encode($employees) .'}'; 
} catch(PDOException $e) {
  echo '{"error":{"text":'. $e->getMessage() .'}}'; 
}

Here the client receives all the data and outputs it to the block.
function getEmployeeList() {
  $('#busy').show();
  $.getJSON(serviceURL + 'getemployees.php', function(data) {
    $('#busy').hide();
    $('#employeeList li').remove();
    employees = data.items;
    $.each(employees, function(index, employee) {
      $('#employeeList').append('<a href="employeedetails.html?id=' + employee.id + 
      '"><div class="new"><div class="newname">' + employee.firstName + ' ' + employee.lastName + 
      '</div><div class="pretext2"><table><tr><td style="width:60px; text-align:center;">200р<br><div style="border:1px solid black; border-radius:4px; padding:3px; margin-top:3px;">Купить</div></td><td>' + 
      employee.title + ' ' + employee.reportCount + 
      '</td></tr></table></div></div>');
    });
    setTimeout(function(){
      scroll.refresh();
    });
  });
}

However, what I get at the output is not entirely pleasing to me:
bfbbd5d4417e4c9bb47fa5da9da60893.png
How to overcome the issue with the encoding in this case?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vit, 2014-07-21
@PetuninNikita

> I tried to load the script with a browser and saw that everything was readable.
It doesn't mean anything yet. Your browser may have interpreted the text as cp1251 (which it probably is).
Make sure that the server firstly returns the desired header with encoding, and secondly, the text in the desired encoding.

D
Dmitry Entelis, 2014-07-21
@DmitriyEntelis

Most likely you have a problem with mysql encoding.
Set names utf-8 and also look at the encoding of the table itself.
Well, it's not very clear why you are doing str_replace

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question