我尝试编写一个简单的php脚本,获取一个字符串并在json中返回字符串。
根据本文设置了所有数据库和表单以及html。 http://kunststube.net/frontback/
它很有效:
BUT。当我用JSON打印这个字符串时:
$data = array('success'=> true,'message'=>$row['phrase']);
echo json_encode($data);
它返回给我这样的{"success":true,"message":"\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u043e\u0438\u0442"}
当我打印
时echo $row['phrase'];
它返回俄语/日语/无论
的正常字符串有什么建议吗?
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 更新\\\\\\\\\\\\\\\\\\\ ////////////////////////////////////////////////// //////////
我有一个领域
当字段更改时,此功能执行
$(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');
}
}
});
}
这是我的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);
但我总是收到undefied11111警报(根据javascript)
所以我创造了新领域
<form method="post" id="wer" action="restURL.php" accept-charset="utf-8">
<input name="www" style="position: relative;" lang="ru" />
<input type="submit">
</form>
它返回我{“success”:true,“message”:“\ u0441 \ u043a \ u043e \ u043b \ u044c \ u043a \ u043e \ u0441 \ u0442 \ u043e \ u0438 \ u0442”}当字符串适合时
答案 0 :(得分:1)
在JSON中,\uXXXX
是一种编码unicode字符的方法,因此生成的字符串是ASCII安全的。
例如\u0441
只是西里尔文小写字母es。严格来说,你不应该对这些字符进行编码,但它可能更安全。