2013-05-12 30 views
0

在這裏工作是我的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的問題。

有什麼建議嗎?

感謝

+0

是否'VAR POSTDATA =(!){字符串:字符串}; '修理它?你的字符串可能有特殊字符,並且你沒有正確編碼。 – Barmar 2013-05-12 20:09:38

+0

另外,我不認爲PHP理解JSON格式提交的表單,所以你應該刪除'overrideMimeType'的東西。 – Barmar 2013-05-12 20:12:35

+0

它明白http://www.islandsmooth.com/2010/04/send-and-receive-json-data-using-ajax-jquery-and-php/ – 2013-05-12 20:23:29

回答

0

只需卸下datatype="json"位和更改數據位data: { "string": string }

之後嘗試print_r(json_decode($_POST['string']));。我很確定這會爲你提供數據。

確實刪除您的beforeSend回調。

+0

沒有必要刪除'dataType:「json」'。這是多餘的,因爲PHP設置了Content-Type,但沒有任何傷害。 – Barmar 2013-05-12 20:29:33

+0

冗餘代碼應該總是被刪除:)我明白他想要做什麼。這是一個REST服務,所以他試圖發佈原始json而不將其包裝到JavaScript框架通常所做的形式urlencoded數據中。然後用'file_get_contents(「php:// input」);'在PHP/REST端讀取。更容易在PHP端做出例外,然後在javascript端進行更改:) – 2013-05-12 20:33:20

+0

@DamienOvereem print_r(json_decode($ _ POST ['string']));對我來說並不好,因爲我需要返回多個參數, 數組最後會是這樣的: $ data = array('success'=> true,'message'=> $ row ['phrase'],'timeb'=>'13.04','timee'=>'12.03','animation'=>'go_left'); – 2013-05-12 20:42:40

0

我認爲問題是代碼var postData ="string="+string; jQuery希望這是一個適當的JSON對象。

Next:$string = $_POST['www'];從您的發佈請求中獲取名爲「www」的參數,但上面的名稱是「string」(至少)。

嘗試無論是這樣的:

var getData ="www="+string; 

    $.ajax({ 
     type: "POST", 
     dataType: "json", 
     data: null, 
     beforeSend: function(x) { 
      if(x && x.overrideMimeType) { 
       x.overrideMimeType("application/json;charset=UTF-8"); 
      } 
     }, 
     url: 'restURL.php?' + getData, 

和服務器:

$string = $_GET['www']; 

或本(PHP)

$string = $_POST['string']; 
$stringData = json_decode($string); 

// catch any errors .... 

$quest=$stringData[....whatever index that is...];