Javascript SyntaxError: JSON.parse: JSON 数据第 1 行第 1 列的数据意外结束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27807266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
提问by z470
Okay so I'm trying to pull data from a json file that holds the status of a few LEDs, etc. I have a script that runs a few times a second and pulls the data and the webpage loads it. The problem is, after about 20+ times of the server reading the json file, eventually it will throw this error.
好的,所以我正在尝试从保存几个 LED 等状态的 json 文件中提取数据。我有一个脚本,它每秒运行几次并提取数据,然后网页加载它。问题是,在服务器读取 json 文件大约 20 多次之后,最终它会抛出这个错误。
SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
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);
}
});
})();
});
The json file looks like this:
json 文件如下所示:
{ "led_1": "on", "led_2": "on", "led_3": "on" }
It seems to me that the json data is always properly formatted. I'm not understanding where the error is coming from. Any ideas?
在我看来,json 数据的格式总是正确的。我不明白错误来自哪里。有任何想法吗?
回答by zgr024
Use the "dataType" setting to identify the type of response so the .ajax() call knows its JSON and doesn't need to guess.
使用“dataType”设置来识别响应的类型,这样 .ajax() 调用就知道它的 JSON 并且不需要猜测。
This may not solve the issue as it is likely your response is not returning JSON for the call that is throwing the error. If you add the error setting, you can see what the server is returning on error but if the requests completes, check the console for what is coming back from the server. As I said, its likely not JSON if you are getting that error from $.parseJSON() to begin with.
这可能无法解决问题,因为您的响应可能没有为引发错误的调用返回 JSON。如果添加错误设置,您可以看到服务器在错误时返回的内容,但如果请求完成,请检查控制台以了解从服务器返回的内容。正如我所说,如果您从 $.parseJSON() 开始收到该错误,它可能不是 JSON。
$(document).ready(function() {
(function worker() {
$.ajax({
url: 'server_info.json',
dataType: 'json',
success: function(data) {
console.log(data);
if (data.led_1 == "off") {
// do stuff
}
if (data.led_2 == "off") {
// do stuff
}
if (data.led_3 == "off") {
// do stuff
}
},
error: function( data, status, error ) {
console.log(data);
console.log(status);
console.log(error);
},
complete: function() {
// Schedule the next request when the current one's complete
setTimeout(worker, 250);
}
});
})();
});
回答by eaglei22
Using Firefox debugging, I was able to see that the value type coming back was undefined when I would get that error. Doing a check for a null/undefined on the value to be parsed first, then if condition is false, proceed with parse, else handle condition resolved my issue.
使用 Firefox 调试,我能够看到返回的值类型在我收到该错误时是未定义的。首先检查要解析的值是否为空/未定义,然后如果条件为假,则继续解析,否则处理条件解决了我的问题。
回答by james ace
Turns out, the error is nothing to do with JSON. It's actually because of the backend returning either a none JSON or empty string.
事实证明,该错误与 JSON 无关。这实际上是因为后端返回无 JSON 或空字符串。
// Update
if(updateFile($id,$author)) {
echo json_encode(
array('message' => 'Post Updated')
);
} else {
echo json_encode(
array('message' => 'Post Not Updated')
);
}
As for me, this worked. Goodluck
至于我,这奏效了。祝你好运

