-1

次の JavaScript エラーが発生します。

Chrome の場合:

 Uncaught SyntaxError: Unexpected identifier

および Firefox の場合:

SyntaxError: missing ] after element list

次のスクリプトを使用して、PHPからJSONデータを取得するajax クエリを作成しています。

<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でテストすると有効と思われます。その背後にある理由は何ですか?

4

1 に答える 1

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;
}); 
于 2013-05-24T11:42:19.110 に答える