2014-02-06 38 views
0

我試圖發送一個數組從PHP到Javascript使用AJAX。該數組由從數據庫中檢索的字段組成。 我嘗試使用JSON_encode發送數組,但它返回一個空值。我在網上閱讀,發現問題是內容類型。內容類型必須設置爲文本/ JSON。我嘗試了在javascript中使用AJAX調用所需的文本/ XML(我嘗試用text/json替換文本/ xml,但後來xmlhttpObject變爲null) 這裏是PHP代碼:JSON編碼不工作在AJAX

<?php 

header('Content-Type: text/xml'); 
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'; 
echo '<response>'; 
require 'connect.inc.php'; 
$sub=$_GET['sub']; 
$no=$_GET['no']; 

$query="SELECT * FROM ".$sub." WHERE id='".$no."'"; 


if($query_run=mysql_query($query)) { 
    if($query_row=mysql_fetch_assoc($query_run)) { 
     $ques=$query_row['question']; 
     $op1=$query_row['op1']; 
     $op2=$query_row['op2']; 
     $op3=$query_row['op3']; 
     $op4=$query_row['op4']; 
     $ans=$query_row['ans']; 
     $result = mysql_query("select count(1) FROM ".$sub); 
     $row = mysql_fetch_array($result); 
     $total = $row[0]; 
     $array=array($ques,$op1,$op2,$op3,$op4,$ans,$total,$id); 


     echo json_encode($array); 
    } 
} else { 
    echo mysql_error(); 
}  
echo'</response>'; 
?> 

這裏是JavaScript代碼段,其處理服務器響應:

function handleServerResponse() { 
    if (xmlHttp1.readyState == 4) { 
     if (xmlHttp1.status == 200) { 
      xmlResponse = xmlHttp1.responseXML; 
      xmlDocumentElement = xmlResponse.documentElement; 
      message = xmlDocumentElement.firstChild.data; 
      array = JSON.parse(message); 
      document.getElementById('question').innerHTML = array[0]; 
      document.getElementById('option1').innerHTML = array[1]; 
      document.getElementById('option2').innerHTML = array[2]; 
      document.getElementById('option3').innerHTML = array[3]; 
      document.getElementById('option4').innerHTML = array[4]; 
      answerArray[qno] = array[5]; 
      noOfQuest = array[6]; 
     } 
     else { 
      alert('Something went wrong!'); 
     } 
    } 
} 

陣列= JSON.parse(消息)返回null。我認爲那是因爲消息是空的。 我該如何解決這個問題?

非常感謝

更新:我查過 '信息',它說不確定。 我也試圖從PHP派單值不JSON編碼爲JS,它仍然給未定義

+0

什麼是'message'變量? – zerkms

+0

對不起,其實消息變量返回null ...我會更新問題 有沒有一些線我已經被遺漏了? – anibjoshi

回答

0

如果你想只JSON輸出結果,你必須改變你的PHP文件中的一些線條狀

見最後五行):

<?php 

header('Content-Type: text/xml'); 
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'; 
echo '<response>'; 
require 'connect.inc.php'; 
$sub=$_GET['sub']; 
$no=$_GET['no']; 

$query="SELECT * FROM ".$sub." WHERE id='".$no."'"; 


if($query_run=mysql_query($query)) { 
    if($query_row=mysql_fetch_assoc($query_run)) { 
     $ques=$query_row['question']; 
     $op1=$query_row['op1']; 
     $op2=$query_row['op2']; 
     $op3=$query_row['op3']; 
     $op4=$query_row['op4']; 
     $ans=$query_row['ans']; 
     $result = mysql_query("select count(1) FROM ".$sub); 
     $row = mysql_fetch_array($result); 
     $total = $row[0]; 
     $array=array($ques,$op1,$op2,$op3,$op4,$ans,$total,$id); 
    } 
} else { 
    $array = array('result' => 'error', 'message' => mysql_error()); 
} 
echo json_encode($array); 
?> 
0

Ajax中X是很大的欺騙性, 「AJAX」 請求不必使用XML;實際上幾乎沒有現代代碼使用XML來支持AJAX。

XML和JSON是兩種不同的數據交換格式。將它編碼爲一個或另一個。 JSON將是最好的,因爲在JavaScript和大多數其他語言中讀取和寫入都非常容易。

刪除PHP中的所有XML內容,發送爲application/json並使用responseText來檢索它,而不是responseXML

<?php 

header('Content-Type: application/json'); 

require 'connect.inc.php'; 
$sub=$_GET['sub']; 
$no=$_GET['no']; 

// VERY IMPORTANT, escape all uses provided data before using it in a query!!! 
$sub=mysql_real_escape_string($sub); 
$no=mysql_real_escape_string($no); 

$query="SELECT * FROM ".$sub." WHERE id='".$no."'"; 

if($query_run=mysql_query($query)) { 
    if($query_row=mysql_fetch_assoc($query_run)) { 
     $ques=$query_row['question']; 
     $op1=$query_row['op1']; 
     $op2=$query_row['op2']; 
     $op3=$query_row['op3']; 
     $op4=$query_row['op4']; 
     $ans=$query_row['ans']; 
     $result = mysql_query("select count(1) FROM ".$sub); 
     $row = mysql_fetch_array($result); 
     $total = $row[0]; 
     $array=array($ques,$op1,$op2,$op3,$op4,$ans,$total,$id); 

     echo json_encode($array); 
    } 
} else { 
    // send a 500 HTTP error code so the browser knows there was an error 
    // when handling the AJAX. 
    header('HTTP/1.1 500 Internal Server Error'); 
    echo mysql_error(); 
}  
?> 

你會注意到我用`mysql_real_escape_string來逃避用戶提供的數據。這樣做對於避免SQL injection attacks非常重要。如果切換到mysqli並使用其執行prepared statements以確保在查詢中未意外使用未轉義的用戶數據,將會更好。

function handleServerResponse() { 
    if (xmlHttp1.readyState == 4) { 
     if (xmlHttp1.status == 200) { 
      // make sure to declare your local variable with the var statement 
      // or they will be implied globals! 
      var json = xmlHttp1.responseText; 
      var array = JSON.parse(json); 
      document.getElementById('question').innerHTML = array[0]; 
      document.getElementById('option1').innerHTML = array[1]; 
      document.getElementById('option2').innerHTML = array[2]; 
      document.getElementById('option3').innerHTML = array[3]; 
      document.getElementById('option4').innerHTML = array[4]; 
      answerArray[qno] = array[5]; 
      noOfQuest = array[6]; 
     } 
     else { 
      alert('Something went wrong!'); 
     } 
    } 
}