When I use json_encode($array) I get the data properly but when I use json_encode within an array which is looped I Get the following error
[object Object] parsererror SyntaxError: Unexpected token {
I'm using ajax to get json data from functions.php
$(function() {
$('#get').click(function(){
$.ajax({
url: 'http://android.ezinfotec.com/functions.php',
type : 'GET',
data : 'method=getquestions',
dataType : 'json',
success : function(s) {
console.log(s);
},
error: function(XMLHttpRequest,textStatus,errorThrown)
{
console.log(XMLHttpRequest+' '+textStatus+' '+errorThrown);
}
});
});
});
The functions.php
<?php
header('Content-type: application/json');
include("connect.php");
if($_GET['method'] == 'getquestions')
{
$query = mysql_query("select * from questions");
while($fetch = mysql_fetch_array($query))
{
$output = array(
"id" => $fetch['id'],
"answers" => $fetch['answers'],
"status" => $fetch['ans_status'],
"postedon" => substr($fetch['month'],0,3).' '.$fetch['day'].' '.$fetch['year'],
"question" => $fetch['question'],
"category" => $fetch['category'],
"parent" => $fetch['parentcategory'],
"authorid" => $fetch['author'],
"authorname" => $fetch['author_name']
);
echo json_encode($output);
}
}
In the above php code if I remove the while loop and simple define custom values to the variable i get perfect data in the html page.
Note: There is no cross domain issue as I have many functions working except for getquestions();
You can check the json output at http://android.ezinfotec.com/functions.php?method=getquestions
mysql_is depreciated...usemysqli_instead!