为什么$ .getJSON不在回调中执行代码?

时间:2013-05-23 14:51:15

标签: jquery getjson

我有以下JQuery:

        <script>
        $(document).ready(init);
        function init() {
            console.log('before');
            $.getJSON('data.txt', function(data) {
                console.log('inside');
                alert(data);
            });
            console.log('after');

        }
    </script>   

data.txt看起来像这样:

{
    'one','111',
    'two','222',
    'three','333'
}

但是在控制台窗口中,我得到:

before    
after

为什么回调中的代码没有执行?

附录

@newman,在Chrome的网络标签中,这是data.txt文件:

enter image description here

附录2

在Firefox中,它表示它是在“之前”和“之后”之间读取它,但从不在控制台中打印“内部”或执行警报

enter image description here

1 个答案:

答案 0 :(得分:4)

看起来它是默默失败的。 data.txt的内容不是格式良好的JSON。它应该是:

{
    "one":"111",
    "two":"222",
    "three":"333"
}

如果找到data.txt并返回并成功解析了其内容,则应该进入控制台:

before
after
inside
相关问题