javascript - json encode not working in ajax -


im trying send array php javascript using ajax. array consists of fields retrieved database. tried using json_encode sending array returns null value. read on internet , found problem content-type. content type has set text/json. using text/xml in php code assume necessary ajax call in javascript.(i tried replacing text/xml text/json xmlhttpobject became null) here php code:

<?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 * ".$sub." 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) ".$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>'; ?> 

and here javascript snippet handles server response:

    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!');         }     } } 

array=json.parse(message) returns null. think because message null. how solve problem?

thanks lot

update: checked 'message', says undefined. tried send single value php js without json encode, still gives undefined

if want json output result, have change lines in php file like

(see last 5 lines):

<?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 * ".$sub." 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) ".$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); ?> 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -