javascript - Uncaught SyntaxError : Unexpected token::

标签 javascript syntax-error mime getjson

在我的应用程序中,当使用getJSON从服务器获取JSON时,出现Uncaught SyntaxError:Unexpected token:

这是我使用的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。

最佳答案

意外 token 不是:,而是该消息中:后面的空白。

您需要删除内部对象中的最后一个逗号。 Internet Explorer特别针对这些尾随逗号:逗号表示“还有其他内容”,而其他浏览器使用逗号表示“这就是结尾”。

由于您使用的json_encode应该自动处理,因此您可能会向其提供错误的数据。检查查询返回的数据:也许有一个空行?

关于javascript - Uncaught SyntaxError : Unexpected token::,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10510716/

相关文章:

javascript - 如何将此 Javascript 分解为单独的 CSS 类?

javascript - 如何将值传递给 "data-value"

javascript - 将 $scope 变量传递到带 Angular html 函数标记中

c# - mysql 更新查询(带有准备好的语句)不起作用

c - 对我有一个库的东西的 undefined reference

php - .htaccess 重定向,MIME 错误

Python 电子邮件库 - 如何从现有邮件中删除附件?

javascript - 以 http ://www. test.com 形式验证 URL

javascript - 当 Node.js 在内部仍然依赖于 Threads 时,它如何天生就更快?

html - MIME 类型 : Linking to CSS w/a txt Extension