0

I'm building an App with React and Mobx which gets served data as JSON from an PHP/MySQL backend. The MySQL-table until now contained only simple varchar-fields and so far it worked ok. Now I added a Date-field to the table, and now I get the error SyntaxError: Unexpected end of JSON input- Why is that?

The PHP Code looks like this:

$app->get('/api/releases', function(Request $request, Response $response) {
$sql = "SELECT * FROM albums";

try{
    $db = new db();

    $db = $db->connect();

    $stmt = $db->query($sql);
    $releases = $stmt->fetchAll(PDO::FETCH_OBJ);
    $db = null;
    echo json_encode($releases);
   } catch(PDOException $e) {
       echo '{"error": {"text": '.$e->getMessage().'}';
     }
});

Should the Date be converted to a string first?

14
  • maybe fetch as an array rather than objects? Commented Sep 25, 2017 at 9:31
  • 1
    first step is to see which string is sent to the client. Try to use the same url in a browser and see the returned string. Commented Sep 25, 2017 at 9:32
  • 1
    Not related to your current problem, but note that your error message is not valid json, you should use json_encode() on an array / object there as well. Commented Sep 25, 2017 at 9:32
  • Possible duplicate of json parsing error syntax error unexpected end of input Commented Sep 25, 2017 at 9:33
  • @GiacomoMasseroniChiaro actually the same url returns nothing BUT when I add an ID of an entry, lets say /api/releases/10 I get the correct data!?! Commented Sep 25, 2017 at 9:40

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.