Chrome:Uncaught SyntaxError:元素列表后面的意外标识符,Firefox:SyntaxError:missing]

时间:2013-05-24 11:38:38

标签: php jquery ajax json

我收到以下JavaScript错误:

在Chrome中:

 Uncaught SyntaxError: Unexpected identifier

并在Firefox中:

SyntaxError: missing ] after element list

我正在使用以下脚本制作 ajax查询以从 PHP 获取 JSON 数据。

<script>
$(document).ready(function(){
        setInterval($.ajax({                                      
            url: 'process.php',
        dataType: 'json',
        success: function(data){
        for(var i=0; i<data.length; i++){
                $("h1").html(data[0]+" "+data[1]);
        }
        }
    }), 5000);
    return false;
}); 
</script>

我收到的 JSON 格式为

["Avg\u00e5ngar:","Resecentrum (V\u00e4xj\u00f6) kl. 13:33","Linje","Destination","N\u00e4sta tur (min)","\u00a0","D\u00e4refter","\u00a0","1","Hovshaga C","57","--","1","Kurortsv\u00e4gen via Hovshaga C","7","18","1","Teleborg","15","54","2","Resecentrum via Araby","12","--","3","Grand Samarkand","17","47","4","H\u00f6gstorp","ca 12","ca 42","4","\u00d6jaby via \u00d6 R\u00e4ppe","18","--","5","Sandsbro","7","37","5","Teleborg","10","40","6","Resecentrum via Norr","27","--","7","Evedal","45","--","7","Universitetet","6","21","7","\u00d6stra Lugnet","11","--","21","Centrum-\u00d6ster-Norr-Centrum","27","--","22","Centrum-Hov-Centrum","47","--","23","Centrum-S\u00f6der-Centrum","14","--","50","Smaland Airport","37","--","114","Gransholm","ca 7","--","123","\u00c4lmhult","ca 32","--","144","Ljungby","ca 37","--","145","Halmstad","27","--","215","Tingsryd","ca 27","--","217","Billa","52","--","218","Kosta","2","ca 52","240","Ronneby","ca 42","--","241","J\u00e4t","ca 57","--","310","\u00c5seda","ca 27","ca 57","331","Fr\u00f6seke","ca 37","--","350","Rottne Bra\u00e5s","ca 37","--"]

jsonlint中测试时似乎有效。它背后的原因可能是什么?

1 个答案:

答案 0 :(得分:1)

在评论中更新了您的问题的答案

$(document).ready(function(){

    callAjax();
    setInterval(function(){
        callAjax();
    }, 5000);

    function callAjax(){
        $.ajax({                                      
            url: 'process.php',
            dataType: 'json',
            success: function(data){
                for(var i=0; i<data.length; i++){
                    $("h1").html(data[0]+" "+data[1]);
                }
            }
        });
    }

    return false;
}); 

旧答案:

请在下次自己查找语法错误,因为SO不适用于此类问题。

$(document).ready(function(){
    setInterval(function(){
        $.ajax({                                      
            url: 'process.php',
            dataType: 'json',
            success: function(data){
                for(var i=0; i<data.length; i++){
                    $("h1").html(data[0]+" "+data[1]);
                }
            }
        });
    }, 5000);
    return false;
});