Karaja
Karaja

Reputation: 415

Uncaught Syntax Error. Unexpexcted end of input

I have this javascript file that is calling a php file to return a JSON string. Chrome dev tools is throwing the error at line 10 in the javascript code. I know the error relates to a missing bracket but for the life of my and 3 other people who have looked at it the syntax is correct.

var request = new XMLHttpRequest();
var listings = [];
var json;
var url = "getListing.php";
request.open("GET", url, true);
request.send();
request.onreadystatechange = function(e)
{
if(request.readyState == 4){
    json = JSON.parse(request.responseText);

    for(var x = 0; x < json.length; x++){
        var list = new listingInfo();
        list.category = json[x].category;
        list.date = json[x].dateListed;
        list.description = json[x].description;
        list.id = json[x].listingID;
        list.title = json[x].title;
        list.userID = json[x].userID;
        listings.push(list);
    }   
}
console.log(listings);
}

here is the php file

<?php
session_start();
$con = mysql_connect("localhost", "listAdmin", "hermes");
if(!$con)
{
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("GregsList", $con)
    or die("Unable to select database:" . mysql_error());

$result = mysql_query("SELECT * FROM Listings WHERE userID = '$_SESSION[userID]' ORDER BY dateListed DESC");

#converts to json
$rows = array();
while($r = mysql_fetch_assoc($result)) 
{
        $rows[] = $r;
}

#If you want to see if correct json is printing use ---> print json_encode($rows);

return json_encode($rows);
?>

Upvotes: 1

Views: 126

Answers (1)

Emil Condrea
Emil Condrea

Reputation: 9973

request.readyState == 4 is not enough you should add request.status== 200 In your php script replace return json_encode($rows); with print json_encode($rows);

Upvotes: 2

Related Questions