小编z47*_*470的帖子

SyntaxError:JSON.parse:JSON数据第1行第1列的意外数据结尾

好的,所以我试图从一个保存了几个LED等状态的json文件中提取数据.我有一个每秒运行几次的脚本并提取数据并且网页加载它.问题是,在服务器读取json文件大约20次以上之后,最终会抛出此错误.

SyntaxError:JSON.parse:JSON数据第1行第1列的意外数据结尾

// For toggling the LED/switch status indicators using the json data
$(document).ready(function() {
    (function worker() {
        $.ajax({
            url: 'server_info.json',
            success: function(data) {
                var json = $.parseJSON(data);
                console.log(json); 
                if (json.led_1 == "off") {
                    // do stuff
                }        
                if (json.led_2 == "off") {
                    // do stuff
                } 
                if (json.led_3 == "off") {
                    // do stuff
                }        
            },
            complete: function() {
                // Schedule the next request when the current one's complete
                setTimeout(worker, 250);
            }
        });
    })();
});
Run Code Online (Sandbox Code Playgroud)

json文件如下所示:

{ "led_1": "on", …
Run Code Online (Sandbox Code Playgroud)

html javascript jquery json

11
推荐指数
3
解决办法
6万
查看次数

标签 统计

html ×1

javascript ×1

jquery ×1

json ×1