Javascript JSON 解析错误:JSON 数据第 1 行第 1 列的数据意外结束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29291222/
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
JSON Parse Error: unexpected end of data at line 1 column 1 of the JSON data
提问by shadoweye14
I have a database, analysis.php and index.php webpages. The analysis.php gets the data from the database, sorts it in a required pattern, and then echoes the json_encode($array);into a divwith the id 'data'. I am trying to get that JSON Data and parse it in the index.php page.
我有一个数据库、 analysis.php 和 index.php 网页。analysis.php 从数据库中获取数据,按照所需的模式对其进行排序,然后将其回显json_encode($array);到divid 为“data”的 a 中。我正在尝试获取该 JSON 数据并在 index.php 页面中对其进行解析。
However I am getting an error. SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
但是我收到一个错误。 SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
I am trying to get this data everytime the user selects an option from a select box.
每次用户从选择框中选择一个选项时,我都试图获取此数据。
My jQuery code is:
我的 jQuery 代码是:
$(document.body).on('change', '.select' , function () {
var identification = $(this).val();
var JSONText = $(this).closest('div[id|="module"]').find('p[id="JSON"]');
JSONText.load('analysis.php?data=' + identification + ' #data');
console.log("JSON Imported: '" + identification + "'");
var obj = jQuery.parseJSON(JSONText.text());
console.log(JSONText.text());
});
EDIT:As you can see I have the snippet console.log(JSON.text());. The output of the JSON that I get is correct. The only issue I think might be is that the quotes are all "instead of the JSON quotes being different from the outer quotes.
编辑:如您所见,我有代码段console.log(JSON.text());。我得到的 JSON 输出是正确的。我认为唯一的问题可能是引号全部"而不是 JSON 引号与外部引号不同。
回答by LJ?
jQuery.loadis asynchronous, you're trying to parse the JSON before its actually loaded. Using jQuery.getJSONloads the content, does the parsing and provides a callback you can bind to.
jQuery.load是异步的,您试图在实际加载之前解析 JSON。使用jQuery.getJSON加载内容,进行解析并提供可以绑定到的回调。
jQuery.load loads the content as HTML and sets the innerHTML of the selected element, you could bind the completehandler here aswell, but you may encounter issues by loading the content as HTML and then using textto retrieve it from the DOM as some parts of your JSON may be interpreted as HTML elements.
jQuery.load 将内容加载为 HTML 并设置所选元素的 innerHTML,您也可以在complete此处绑定处理程序,但是通过将内容加载为 HTML 然后使用text从 DOM 中检索它作为您的某些部分,您可能会遇到问题JSON 可以解释为 HTML 元素。
Better use this:
更好地使用这个:
$(document.body).on('change', '.select' , function () {
var identification = $(this).val();
$.getJSON(
'analysis.php?data=' + identification + ' #data',
function (data) {
console.log(data);
}
);
});
回答by Alex
In addition to LJ_1102's solution, here is a way to fix your current snippet:
除了 LJ_1102 的解决方案之外,这里还有一种修复当前代码段的方法:
JSONText.load('analysis.php?data=' + identification + ' #data', function() {
console.log("JSON Imported: '" + identification + "'");
var obj = jQuery.parseJSON(JSONText.text());
console.log(JSONText.text());
});

