JSON解析错误:JSON数据第1行第1列的数据意外结束 [英] JSON Parse Error: unexpected end of data at line 1 column 1 of the JSON data

查看:625
本文介绍了JSON解析错误:JSON数据第1行第1列的数据意外结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,analysis.php和index.php网页. analysis.php从数据库中获取数据,以所需的模式对其进行排序,然后将json_encode($array);回显为ID为'data'的div.我正在尝试获取JSON数据并在index.php页面中对其进行解析.

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 div with the id 'data'. I am trying to get that JSON Data and parse it in the index.php page.

但是我遇到一个错误. SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

However I am getting an error. 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.

我的jQuery代码是:

My jQuery code is:

$(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());
});

编辑:如您所见,我有代码段console.log(JSON.text());.我得到的JSON的输出是正确的.我认为唯一的问题是引号全为",而不是JSON引号与外部引号不同.

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.

推荐答案

jQuery.load 是异步,您正在尝试在JSON实际加载之前对其进行解析.使用 jQuery.getJSON 加载内容,进行解析并提供可绑定到的回调

jQuery.load is asynchronous, you're trying to parse the JSON before its actually loaded. Using jQuery.getJSON loads the content, does the parsing and provides a callback you can bind to.

jQuery.load将内容加载为HTML并设置所选元素的innerHTML,您也可以在此处绑定complete处理程序,但是通过将内容加载为HTML然后使用从DOM检索它,因为JSON的某些部分可能被解释为HTML元素.

jQuery.load loads the content as HTML and sets the innerHTML of the selected element, you could bind the complete handler here aswell, but you may encounter issues by loading the content as HTML and then using text to retrieve it from the DOM as some parts of your JSON may be interpreted as HTML elements.

最好使用这个:

$(document.body).on('change', '.select' , function () {
    var identification = $(this).val();
    $.getJSON(
        'analysis.php?data=' + identification + ' #data',
        function (data) {
           console.log(data);
        }
    );
});

这篇关于JSON解析错误:JSON数据第1行第1列的数据意外结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆