The app that I am developing has a log in function that greets the users that successfully have filled in their Username and Password with a welcoming alert message like for instance: Welcome Steve. My app is developed mainly for a Norwegian audience and since we have some special characters like Æ, Ø and Å, I want those characters to be preserved in my apps greetings as well. As it works at this point, I am greeted with Welcome \u00c5ge because my first name is Åge. I need a way to preserve special characters in my json responseText. Can this be done in my auth.php(server script) or when received in loginReq(Titanium) or do both need a modification?
Here are my some of my code:
From server script(auth.php):
if (mysql_num_rows($query) > 0) { $row = mysql_fetch_array($query); $response = array( 'auth' => true, 'message' => _('Welcome').' '.$row['first_name'].' '.$row['last_name'], 'username' => $row['username'], 'password' => $row['password'], // 'first_name' => $row['first_name'], 'last_name' => $row['last_name'] ); $response = json_encode(utf8ArrayValues($response)); // All values must be in utf8 before encoded through json_encode echo $response = str_replace("\\", "\\\\", $response); // Needs this to preserve unicoded special characters } else { // Else the username and/or password was invalid! Create an array, json_encode it and echo it out $response = array( 'auth' => false, 'message' => _('Sorry, wrong Username or Password!') ); echo json_encode($response); }Script receiving the $response(Titanium):
loginReq.onload = function(){ var json = this.responseText; var response = JSON.parse(json); if (response.auth == true) { alert(response.message); }else{ alert(response.message); } };
1 Answer
Accepted Answer
Hi Åge
I have made a simplified version of your php file for testing purposes and swapped where the encoding occurs.
I have used utf8_encode around the fields which require encoding - this functions at a string level. I have also used the header call for UTF8.
I know you are using an array from sql using mysql_fetch_array, and I wonder if that is where you are actually having problems. I have used a manual array for $row as I do not have access to your database.
The example below works correctly and passes the correctly encoded string \u00c5 in this case.
<?php header( 'Content-Type: text/html; charset=utf-8' ); $row = array('username' => 'user', 'password' => 'passw1', 'first_name' => utf8_encode('Åge'), 'last_name' => utf8_encode('Engjom')); $response = array( 'auth' => true, 'message' => _('Welcome').' '.$row['first_name'].' '.$row['last_name'], 'username' => $row['username'], 'password' => $row['password'], 'first_name' => $row['first_name'], 'last_name' => $row['last_name'] ); echo json_encode($response); ?>My HTTPClient is almost identical to yours.
xhr.onload = function (e) { switch (xhr.status) { case 200: response = this.responseText; json = JSON.parse(response); alert(json.message); break; case 304: break; } };
Your Answer
Think you can help? Login to answer this question!