Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am trying to display Data in MySQL DB. But, I keep getting SyntaxError: Unexpected end of JSON input at Object.parse (native) .

My PHP file which fetch the data is

<?php 
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8"); 

include_once 'config/database.php'; 
include_once 'objects/ideas.php'; 

$database = new Database(); 
$db = $database->getConnection();

$ideas = new ideas($db);

$stmt = $ideas->readAll();
$num = $stmt->rowCount();

if($num>0){

    $data="";
    $x=1;


    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        // extract row
        // this will make $row['name'] to
        // just $name only
        extract($row);

        $data .= '{';
            $data .= '"id":"'  . $id . '",';
            $data .= '"name":"' . $name . '",';
            $data .= '"description":"' . html_entity_decode($description) . '",';
            $data .= '"SubmitedBy":"' . $SubmitedBy  . '",';
        $data .= '}'; 

        $data .= $x<$num ? ',' : ''; $x++; } 
} 

// json format output 
echo '{"records":[' . $data . ']}'; 
?>

My AngularJS Code,

 $scope.getAll = function() {
                $http.get("read_ideas.php").success(function(response) {
                    $scope.names = response.records;
                });
            }

And the Data in MySQL DB is just

id - 1
name - me
description - desc
SubmitedBy - me
created - 2016-05-19 14:13:10
modified - 2016-05-19 17:43:10

I am not able to figure out why it throws this error again and again.

i understand it is somehow not able to parse the data in db to JSON format. But, how can i fix it.

share|improve this question
3  
What does the resulting JSON look like? Have you tested it is valid? Why on earth are you generating it by bashing strings together instead of using json_encode?! – Quentin May 19 '16 at 12:34
    
jsonlint.com is good online tool to check your JSON structure. – mitkosoft May 19 '16 at 12:38
    
Tried the 'echo json_encode($row);' still its not working. – shubham deodia May 19 '16 at 12:41
1  
You actually have an extra , at the end of your last object, SubmitedBy x) you have to remove it, so it should be $data .= '"SubmitedBy":"' . $SubmitedBy . '"; – Miguel Guerreiro May 19 '16 at 12:45
    
@MiguelGuerreiro, thanks it worked. – shubham deodia May 20 '16 at 6:26
up vote 0 down vote accepted

You actually have an extra , at the end of your last object, SubmitedBy x) you have to remove it, so it should be

 $data .= '"SubmitedBy":"' . $SubmitedBy . '";
share|improve this answer

You are actually adding a , at the last value of SubmitedBy replace this

$data .= '"SubmitedBy":"' . $SubmitedBy  . '",';

with this

$data .= '"SubmitedBy":"' . $SubmitedBy;
share|improve this answer
    
I tried, still its giving me below error angular.min.js:117 SyntaxError: Unexpected end of JSON input at Object.parse (native) – shubham deodia May 19 '16 at 12:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.