getJSON没有返回数据

时间:2013-02-23 04:34:33

标签: jquery ajax json

我正在尝试做一个基本的getJSON(),但我相信我没有得到任何结果。

这是我的HTML / js

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Management Sheet</title>
        <meta name="description" content="  ">
        <meta name="keywords" content="   ">
        <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
        <script>
            $(document).ready(function(){
                $("button").click(function(){
                    $.getJSON("config.json",function(result){
                        $.each(result, function(i, field){
                            $("div").append(field + " ");
                        });
                    });
                });
            });
        </script>
    </head>

    <body>
        <section>
            <button>Actually Click me</button>
        </section>
        <div>
        </div>
    </body>
</html>

和我的json

{
    'line1':'Mike B so cool',
    'line2':'Type your namasd',
    'line3':'763-345',
    'num_rows':'15',
    'num_cols':'3',
    'bgColorPage':'#f08008',
    'bgColorFilled':'#08f008',
    'bgColorEmpty':'#6f00ff'
}

当我点击我的按钮时,我没有错误,但我的div没有附加任何内容。

3 个答案:

答案 0 :(得分:1)

尝试

{
   "line1": "MikeBsocool",
   "line2": "Typeyournamasd",
   "line3": "763-345",
   "num_rows": "15",
   "num_cols": "3",
   "bgColorPage": "#f08008",
   "bgColorFilled": "#08f008",
   "bgColorEmpty": "#6f00ff"
}

使用上面的json对象并查看。

你的代码是完美的,你的代码在json中使用上面的对象没有任何问题,并测试它你保证会成功。

答案 1 :(得分:0)

尝试使用text();

$.getJSON("config.json",function(result){
    $.each(result, function(i, field){
        console.log();
        $("div").text(field + " ");
    });
});

答案 2 :(得分:0)

我尝试了你的脚本,我不知道为什么,但如果你改变双引号的单引号:

 {
  "line1":"Mike B so cool",
  "line2":"Type your namasd",
  "line3":"763-345",
  "num_rows":"15",
  "num_cols":"3",
  "bgColorPage":"#f08008",
  "bgColorFilled":"#08f008",
  "bgColorEmpty":"#6f00ff"
 }
相关问题