I trying write a simple php script witch get a string and return string in json.
All database and forms and html was set according to this article. http://kunststube.net/frontback/
And it works great:
- The form send string to php with a text writen in russian/japanese/whatever,
- Then it find the similar string in the database.
BUT. When i print this string with JSON:
$data = array('success'=> true,'message'=>$row['phrase']);
echo json_encode($data);
It returns to me something like this {"success":true,"message":"\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u043e\u0438\u0442"}
When i print
echo $row['phrase'];
it returns normal string in russian/japanese/whatever
Any suggestions?
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\\\\\\\ UPDATE \\\\\\\\\\\\\\\\\\\ ////////////////////////////////////////////////////////////
I have a field
When the field is changes this function execute
$(document).ready(function(){
$('#mike').bind('webkitspeechchange',function()
{
a= $(this).val();
recognizeAjax(a);
}) ;
});
function recognizeAjax(string) {
var postData ="string="+string;
$.ajax({
type: "POST",
dataType: "text",
data: postData,
beforeSend: function(x) {
if(x && x.overrideMimeType) {
x.overrideMimeType("application/json;charset=UTF-8");
}
},
url: 'restURL.php',
success: function(data) {
// 'data' is a JSON object which we can access directly.
// Evaluate the data.success member and do something appropriate...
if (data.success == true){
alert(data.message);
}
else{
alert(data.message+'11111');
}
}
});
}
And here is my php
<?php header('Content-type: application/json; charset=utf-8');
error_reporting(E_ALL);
ini_set('display_errors', true);
// Here's the argument from the client.
$string = $_POST['string'];
$quest=1;
//$quest=$_POST['quest'];
//$event=$_POST['event'];
//$success = processDomain($string);
//!!!!!!!!!!!!! PLEASE SAY NOTHING ABOUT THE WAY I CONNECT it doesn't metter right now!!!!
$con=mysql_connect("localhost", "*******", "*****") or die(mysql_error());
mysql_select_db("vocabulary", $con) or die(mysql_error());
mysql_set_charset('utf8', $con);
$sql="SELECT * FROM `0` WHERE event_name = 'taxi' AND quest_id = '".$quest."'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
if ($string == htmlspecialchars($row['phrase']))
{
// $sql="SELECT * FROM `1` WHERE step_id = '".$row['step_id']."' AND quest_id = '".$quest."'";
// $result = mysql_query($sql);
// $answer = mysql_fetch_array($result);
// Set up associative array
$data = array('success'=> true,'message'=>$row['phrase']);
//echo $row['phrase'];
// JSON encode and send back to the server
echo json_encode($data);
break;
} else {
// Set up associative array
$data = array('success'=> false,'message'=>'aint no sunshine');
echo json_encode($data);
break;
}
}
mysql_close($con);
But i always receive undefied11111 alert (according to javascript)
So i made new field
<form method="post" id="wer" action="restURL.php" accept-charset="utf-8">
<input name="www" style="position: relative;" lang="ru" />
<input type="submit">
</form>
And it returns me {"success":true,"message":"\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u043e\u0438\u0442"} When the string is fit.
1 Answer 1
In JSON, \uXXXX is a way of encoding unicode characters so the resulting string is ASCII safe.
For example \u0441 is just the cyrillic small letter es. Strictly speaking you shouldn't need to encode these characters, but it's probably safer that way.
header('Content-type: application/json');. There's nocharsetoption for JSON, the encoding has to follow the JSON specification.JSON.parse()in the browser will decode the characters properly.