Uncaught SyntaxError: Unexpected token ::
In my application I get a Uncaught SyntaxError: Unexpected token : when using getJSON to fetch JSON from server.
This is the javascript I use:
$('#employeeListPage').bind('pageinit', function(event) {
getEmployeeList();
});
setInterval ( "getEmployeeList()", 10000 );
var vanhadata = "";
function getEmployeeList() {
$.getJSON(serviceURL + 'getemployees.php?autonumero=' + autonumero + 'callback=?', function(data) {
if(JSON.stringify(data) != JSON.stringify(vanhadata)){
$('#employeeList li').remove();
employees = data.key;
$.each(employees, function(index, employee) {
$('#employeeList').append('<li><a href="keikka.html?id=' + employee.IND + '">' +
'<h4>' + employee.OSO + '</h4>' +
'<img src="pics/' + employee.TILA + '.png"/>' +
'<p>' + employee.AIKA + '</p>' +'</a></li>');
});
$('#employeeList').listview('refresh');
if(vanhadata != "")
alert("Uusia keikkoja!");
vanhadata = data;
}
});
}
The JSON response from the server seems to be right but it shows error and no data is displayed. Console also prints: "Resource interpreted as Script but transferred with MIME type text/html:"
This is the JSON response:
{
"key": [
{
"NIMET": "Tuntematon",
"PERHJAS": "0",
"SAATTAJA": "0",
"m_yht": "0",
"OSO": null,
"AIKA": "2010-03-11 10:00:00",
"OSOITELAJI": "0",
}
]
}UncaughtSyntaxError: Unexpectedtoken:
And the getemployees.php:
<?php
header('Content-Type: application/json; charset=UTF-8');
include 'config.php';
$number = $_GET['autonumero'] ;
$sql = "select * from table where number="$number"";
try {
$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->query($sql);
$employees = $stmt->fetchAll(PDO::FETCH_OBJ);
$dbh = null;
echo '{"key":'. json_encode($employees) .'}';
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
?>
When modify PHP file to:
if ($_GET['callback'])
print $_GET['callback']."(";
echo '{"key":'. json_encode($results) .'}';
if ($_GET['callback'])
print ")";
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
The response I get is:
<br />
<font size='1'><table class='xdebug-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Parse error: syntax error, unexpected T_CATCH in C:Servicesgetemployees.php on line <i>40</i></th></tr>
</table></font>
So it is not JSON at all.
The unexpected token is not the :
, it's the blank following the :
in that message.
You need to remove the final comma in your inner object. Internet Explorer in particular objects to these trailing commas: it appears a comma means "There's something else to come," whereas other browsers use the comma to mean "That's the end of that one".
Because you're using json_encode
that should be handled automatically, so you may be feeding it bad data. Check the data that's returned from your query: is there an empty row, perhaps?
强制getemployees.php打印application/json
的内容类型而不是纯文本
上一篇: 用gmaps4rails显示多边形