在這裏工作是我的HTML數據類型JSON沒有用PHP
<input x-webkit-speech id="mike" name="string" style="position: relative;" disabled lang="ru" />
然後當場的變化,
此函數執行
$(document).ready(function(){
$('#mike').bind('webkitspeechchange',function()
{
a= $(this).val();
recognizeAjax(a);
}) ;
});
function recognizeAjax(string) {
var postData ="string="+string;
$.ajax({
type: "POST",
dataType: "json",
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+'hy');
}
}
});
這裏是我的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['www'];
$quest=1;
$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 `text` WHERE event_name = 'taxi' AND quest_id = '".$quest."'";
$result = mysql_query($sql);
mysql_close($con);
while($row = mysql_fetch_array($result))
{
if ($string == htmlspecialchars($row['phrase']))
{
$data = array('success'=> true,'message'=>$row['phrase']);
// JSON encode and send back to the server
header("Content-Type: application/json", true);
echo json_encode($data);
exit;
break;
} else {
// Set up associative array
$data = array('success'=> false,'message'=>'aint no sunshine');
header("Content-Type: application/json", true);
echo json_encode($data);
exit;
break;
}
}
當我改變數據類型來在javasript功能「文字」 - 我收到一個警報「undifiend」
但當chenge爲「JSON」 ..我什麼也得不到(鉻debuger什麼也看不到)
我將這篇文章的所有編碼設置爲http://kunststube.net/frontback/ 我用簡單的POST請求檢查它 - 它工作的很完美。
json的問題。
有什麼建議嗎?
感謝
是否'VAR POSTDATA =(!){字符串:字符串}; '修理它?你的字符串可能有特殊字符,並且你沒有正確編碼。 – Barmar 2013-05-12 20:09:38
另外,我不認爲PHP理解JSON格式提交的表單,所以你應該刪除'overrideMimeType'的東西。 – Barmar 2013-05-12 20:12:35
它明白http://www.islandsmooth.com/2010/04/send-and-receive-json-data-using-ajax-jquery-and-php/ – 2013-05-12 20:23:29