【问题标题】:getJSON no data returnedgetJSON 没有返回数据
【发布时间】:2013-02-23 13:02:43
【问题描述】:

我正在尝试做一个基本的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 没有附加任何内容。

【问题讨论】:

  • 您能否验证 1) 正在发出 ajax 请求以及 2) 来自请求的响应是通过 Chrome Developer ToolsFirebug 之类的指定 JSON?
  • 我可以看出它正在请求正确的页面,但不太确定正确的请求类型
  • 您可以点击左侧面板中的config.json 链接,然后在右侧面板中将显示详细信息,检查标题和响应选项卡
  • 还在回调方法的第一行添加console.log(result)
  • 你的 json 无效签入jsonlint.com

标签: jquery ajax json


【解决方案1】:

试试

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

使用上面的json 对象看看。

您的代码是完美的,您的代码没有任何问题,在 json 中使用上述对象并对其进行测试,您一定会成功。

【讨论】:

    【解决方案2】:

    尝试使用 text();

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

    【讨论】:

    • 仍然没有运气,控制台中什么也没有
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-03
      • 1970-01-01
      • 1970-01-01
      • 2011-02-18
      • 2015-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多