我尝试编写一个简单的 php 脚本,以获取字符串并在 json 中返回字符串。
所有数据库和表单以及 html 都是根据本文设置的。 http://kunststube.net/frontback/
而且效果很好:
- 表单将字符串发送到 php,其中包含用俄语/日语/其他语言编写的文本,
- 然后它在数据库中找到类似的字符串。
但。当我用 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"} 当字符串适合时。