未捕获的SyntaxError:意外的令牌::

时间:2012-05-09 06:15:28

标签: javascript syntax-error mime getjson

在我的应用程序中,我得到一个Uncaught SyntaxError:意外的令牌:当使用getJSON从服务器获取JSON时。

这是我使用的javascript:

$('#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;
        }
    });
}  

来自服务器的JSON响应似乎是正确的,但它显示错误,并且不显示任何数据。控制台还会打印:"Resource interpreted as Script but transferred with MIME type text/html:"

这是JSON响应:

{
    "key": [
        {
            "NIMET": "Tuntematon",
            "PERHJAS": "0",
            "SAATTAJA": "0",
            "m_yht": "0",
            "OSO": null,
            "AIKA": "2010-03-11 10:00:00",
            "OSOITELAJI": "0",

        }
    ]
}UncaughtSyntaxError: Unexpectedtoken: 

和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() .'}}'; 
}


?>

将PHP文件修改为:

           if ($_GET['callback'])
        print $_GET['callback']."(";

    echo '{"key":'. json_encode($results) .'}';  

  if ($_GET['callback'])
        print ")"; 

} catch(PDOException $e) {
    echo '{"error":{"text":'. $e->getMessage() .'}}'; 
}

我得到的回应是:

<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:\Services\getemployees.php on line <i>40</i></th></tr>
</table></font>

所以它根本不是JSON。

2 个答案:

答案 0 :(得分:1)

意外令牌不是:,它是该消息中:之后的空白。

您需要删除内部对象中的最终逗号。特别是Internet Explorer对这些尾随逗号的对象:看起来逗号意味着“还有其他东西要来”,而其他浏览器使用逗号来表示“那就是那个结尾”。

因为您正在使用应自动处理的json_encode,所以您可能会输入错误的数据。检查从查询返回的数据:是否有空行?

答案 1 :(得分:0)

强制您的getemployees.php打印内容类型application/json而不是纯文本