0

My PHP code should return a json object of a sql query, but it always throws the error "unexpected end of input". I noticed it doesn't fail if I don't use the variable $row. Here is a snippet of the code:

PHP:

require("pw.php");

class homework {
    public $fach;
    public $datum;
    public $aufgabe;

    public function __construct($fach, $datum, $aufgabe) {
        $this->fach = $fach;
        $this->datum = $datum;
        $this->aufgabe = $aufgabe;
    }
}

$connection = mysqli_connect($adrs, $usr, $pw, $db);

if(mysqli_connect_errno()) {
    die(json_encode(mysqli_connect_error()));
}

if($_POST["feed"] == "hausaufgaben") {
    $query = "SELECT fach, datum, aufgabe FROM hausaufgaben WHERE fachgruppe != '";
    if($_POST["fremdsprache"] == "latein") {
        $query .= "französisch";
    }
    else {
        $query .= "latein";
    }
    $query .= "' AND fachgruppe != '";
    if($_POST["englisch"] == "koch") {
        $query .= "schopper";
    }
    else {
        $query .= "koch";
    }
    $query .= "' AND datum > '" . date("Y-m-d") . "' ORDER BY datum ASC;";

    $result = $connection->query($query);

    $data = [];

    while($row = $result->fetch_row()) {
        array_push($data, new homework($row[0], $row[1], $row[2]));
    }

    echo json_encode($data);

    $result->close();
}

$connection->close();

?>

Jquery:

$.ajax({
    type: "POST",
    url: "php/getFeed.php",
    cache: false,
    dataType: "json",
    data: {feed: "hausaufgaben", fremdsprache: this.fremdsprache, englisch: this.englisch}
})
.done(function(data) {  
    alert(typeof(data));
    document.getElementById("temp").innerHTML = data;
    if(typeof(data) != "Object") {
        console.log("Error: " + data);
        alert("iwas?");
        this.hausaufgabenError = true;
        this.hausaufgaben = [];
        alert(this.hausaufgaben + "\n\n" + this.hausaufgaben.length);
    }
    else {
        this.hausaufgabenError = false;
        this.hausaufgaben = data;
    }
})
.fail(function(jqXHR, textStatus, error) {
    console.log("error: " + error);
    alert("iwas?");
    this.hausaufgabenError = true;
    this.hausaufgaben = [];
    alert(this.hausaufgaben + "\n\n" + this.hausaufgaben.length);
});

I hope someone can help me out, because I'm completely stuck :(

Edit: In Mozilla, it thows the error SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data.

Sir_baaron
  • 365
  • 1
  • 3
  • 11
  • you missed 2 closing braces } – alamnaryab Sep 07 '15 at 15:57
  • give the entire code, that we can help you : snippet will not help us – Halayem Anis Sep 07 '15 at 15:58
  • @alamnaryab it is a snippet... with 1 missed brace PHP will throw a Parse Error ! – Halayem Anis Sep 07 '15 at 16:01
  • That sounds like an error from jquery, not in php. So you're posting the wrong code, I guess. What we need to know is the actual json that is given out, and what the code is that reads it. Also, it sounds like an error that Chrome gives. Firefox/firebug might give a little more detail. – Chris Lear Sep 07 '15 at 16:01
  • I now updated the PHP code and added the Jquery code. There are some lines that I use for my application, which you can safely ignore. – Sir_baaron Sep 07 '15 at 17:08
  • possible duplicate of [Chrome: Uncaught SyntaxError: Unexpected end of input](http://stackoverflow.com/questions/3594923/chrome-uncaught-syntaxerror-unexpected-end-of-input) – Bogdan Burym Sep 07 '15 at 17:42
  • But how can I fix this? Because when I use strings at new homework("Mathe", "2015-09-23", " Buch") it works – Sir_baaron Sep 08 '15 at 07:47

1 Answers1

0

In jquery, the command $.parseJSON(''); produces the error you are experiencing. Also, parseJSON will fail similarly if it's given something other than JSON to work with. So my guess is that your php page is either displaying an error, returning no output, or returning JSON mixed with other output.

What to do is either:

  • fetch the php page in a browser, not via ajax, and look at the output.

    or

  • change the datatype to text for debugging, and use console.log to look at what is actually being sent to your javascript code.

Community
  • 1
  • 1
Chris Lear
  • 6,592
  • 1
  • 18
  • 26