0

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

6
  • mysql_ is depreciated...use mysqli_ instead! Commented Dec 22, 2013 at 8:52
  • You probably did not read my question fully. Check the last line you can find the json output. Commented Dec 22, 2013 at 8:54
  • why you do not want to send all data at once ? Commented Dec 22, 2013 at 9:05
  • Because this json is sent to an android application. If there is any other method like sending record by record without error please help out. Commented Dec 22, 2013 at 9:06
  • 1
    any specific reason to send individual records ? you can send a set of records, and if android app ask for more then return more, but with this either you need to remember the app state or the app tells on request how many records it had received earlier. Commented Dec 22, 2013 at 9:13

2 Answers 2

3

You'll want to append all of the records to one array and json_encode that instead. It is failing because multiple json objects are being sent back to the page where it expects only one.

$output = array();
while (...) {
  $output[] = ...
}
// add a header too
header('Content-Type: application/json');
echo json_encode($output);

Sorry it's not the complete code. Doing this from my phone is quite fiddly.

Sign up to request clarification or add additional context in comments.

1 Comment

I had the same question to myself. Could you please help me out achieving the same ?
0

I've achieved what I wanted to,

@StuartWakefield thank you for the hint.

I did the following to get proper json result without parseerror

$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);

}

Every record generated using while loop is stored in an other array ($output[]), once the while loop is exhausted I simply encode the $output array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.