【问题标题】:getJSON is not retrieving json objectgetJSON 没有检索 json 对象
【发布时间】:2012-05-04 04:32:49
【问题描述】:

我有一个使用 Django 的 Python 中的 GAE 应用程序。我正在尝试使用 jquery 的 get / getJSON api 从 python api 获取 json,但它不会将传递的字符串读取为 json。我在这里遗漏了什么吗?

Django 模板

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" > </script>
<script type="text/javascript">
    $(document).ready(function(){
    $.get('/test/api?request=getallwords', function(json) {
        $('body').append(json);
        var j = JSON.stringify(json)
        $.each(json.details,function(i){
            $('#list').append($('<li>' + this.word + '</li>'));
        });
        alert(j);
    });
    },'json');
</script>
</head>
<body>
<ol id="list">
</ol>

这里是 python api。

words = Word.all().fetch(20)
for w in words:
   d = {"word":w.name.encode('utf-8'),"lists":[]}
   for l in w.word_lists:
      d["lists"].append({"list":l.word_list.name.encode('utf-8')})
success["details"].append(d)
self.response.headers['Content-Type'] = 'application/javascript'
self.response.out.write(success)

还有json例子

{'response': 'success', 'details': [{'word': 'mcity', 'lists': [{'list': 'infy'}, {'list': 'dasd'} ]}, {'word': 'mcity_1', 'lists': []}, {'word': 'mcity_2', 'lists': []}, {'word': 'mydc', 'lists': [{'list': 'infy'}]}, {'word': 'hyddc', 'lists': [{'list': 'infy'}]}, {'word': 'abysmal', 'lists ': [{'list': 'gre words'}]}, {'word': 'ascent', 'lists': [{'list': 'gre words'}, {'list': 'infy'} , {'list': 'mine'}]}, {'word': 'assda', 'lists': [{'list': 'dasd'}]}]}

【问题讨论】:

    标签: jquery python django google-app-engine google-cloud-datastore


    【解决方案1】:
    $(document).ready(function(){
        $.get('/test/api?request=getallwords', function(json) {
            $('body').append(json);
            var j = JSON.stringify(json)
            $.each(json.details,function(i){
                $('#list').append($('<li>' + this.word + '</li>'));
            });
            alert(j);
        }); // error, you close the $('document').ready() here
        },'json');
    

    应该如下所示:

    $(document).ready(function(){
        $.get('/test/api?request=getallwords', function(json) {
            $('body').append(json);
            var j = JSON.stringify(json)
            $.each(json.details,function(i){
                $('#list').append($('<li>' + this.word + '</li>'));
            });
            alert(j);
        },'json');
     });
    

    【讨论】:

    • 这实际上是问题之一。我不小心删除了 self.response.headers['Content-Type'] = 'application/javascript' 并将 self.response.out.write(success) 更改为 self.response.out.write(json.dumps(success)) 并显示了它。感谢您的帮助!
    【解决方案2】:

    看起来您使用的是 jQuery get() 而不是 getJSON(),因此您需要在请求对象中将 dataType 指定为 JSON。

    如果您认为下面的“json”正在执行此操作,我认为您混淆了右括号(GET 将在 $(document).ready() 之前关闭

    });
        },'json');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多