【问题标题】:Uncaught SyntaxError: Unexpected token ::Uncaught SyntaxError: Unexpected token ::
【发布时间】:2012-05-17 15:37:39
【问题描述】:

在我的应用程序中,我得到一个 Uncaught SyntaxError: Unexpected token : when using getJSON to fetch JSON from server.

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

【问题讨论】:

  • 尝试在 validator 中运行您的 JSON。另外,您可以发布响应文本吗?
  • JSON 对 Internet Explorer 无效。

标签: javascript syntax-error mime getjson


【解决方案1】:

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

您需要删除内部对象中的最后一个逗号。 Internet Explorer 尤其反对这些尾随逗号:它似乎表示“还有其他事情要发生”,而其他浏览器使用逗号表示“那是那个结束”。

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

【讨论】:

  • &lt;?phpheader 之间的空格不会有任何作用,空格必须在 &lt;?php 之前。
  • 似乎没有任何空行
  • @user1323294 导致json_encode 在最后一个逗号和} 之间放置一个空行。那只能是数据。我对空行的猜测只是猜测,因为我不知道数据是什么。
【解决方案2】:

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

【讨论】:

  • 我有 header('Content-Type: application/json; charset=UTF-8');在 PHP 文件中。这将删除解释为脚本但使用 MIME 类型 text/html: 部分传输的资源,但它仍然显示 Uncaught SyntaxError: Unexpected token :
  • 问题在于您的 getemployees.php 脚本,而不是 javascript。可以贴一下代码吗?
  • 嘿伙计,看看这个答案:stackoverflow.com/a/10412923/196917 似乎是你的问题类型。如果解决了,请给他一个upvote
  • 我是 PHP 初学者,所以我真的不知道如何修改我的代码以使其像那样工作。你能给我一个示例代码吗?
  • 你看他在哪里检查if ($_GET['callback']) { print $_GET['callback']."("; }
猜你喜欢
  • 2019-02-10
  • 2014-07-08
  • 2011-03-09
  • 2014-01-06
  • 2012-04-10
  • 1970-01-01
相关资源
最近更新 更多