Django JSON响应不起作用 [英] Django JSON response not working

查看:125
本文介绍了Django JSON响应不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。

我正在一个小型网站上工作,我需要通过JSON发送线程数据。



这是django服务的JSON对象:



 [{  text 一些示例文本  id  21  title  测试线程}] 





我保存为.js文件和以下Javascript代码工作正常:



< script src =   http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js>< /脚本> 
< script>
$( document )。ready( function (){
$(< span class =code-string> button)。click( function ( ){
$ .getJSON( testjson.js function (result){
$ .each(result, function (i,field){
$( div)。append(field.text + );
});
});
});
});
< / script>





但是当我将getJSON网址字段更改为:



http://127.0.0.1:8000/wall/21/ - 格式:http://127.0.0.1:8000 /wall/thread_id



它什么也没显示。 firefox的web控制台也没有显示任何错误。



我的视图代码如下:

  def  thread_view(request,wall,Id):
if request.method == < span class =code-string>' GET'
thread = api.get_thread(wall,Id)
< span class =code-keyword> if thread!=
return HttpResponse(thread,content_type = application / json
else
return HttpResponse( 无结果
else
提高 Http404





它从数据库获取信息并使用简单的序列化器将其格式化为json,然后发送数据。



我缺少什么?



额外信息:



序列化器:

 来自 bson  import  json_util 
class JSONSerializer(object):
def 转储(self,obj):
return json_util.dumps(obj,separators =(' ,'' :'))。encode(' utf-8'

def loading(self,数据):
return json_util.loads(data.decode(' utf-8'))



我正在使用Django 1.6,JQuery 1.1 ,Python 2.7



PS

当我输入以下网址时:http://127.0.0.1:8000 /wall / 21 / firefox显示JSON对象就好了,它与js文件中的那个完全相同。

解决方案

document )。ready( function (){


button)。click( function (){


.getJSON( testjson.js function (结果){

Hello There.
I am working on a small website and I need to send thread-post data through JSON.

This is the JSON object django serves:

[{"text":"Some sample text","id":"21","title":"Test Thread"}]



I saved this as a .js file and the following Javascript code works fine:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $.getJSON("testjson.js",function(result){
      $.each(result, function(i, field){
        $("div").append(field.text + " ");
      });
    });
  });
});
</script>



But when I change the getJSON url field to:

http://127.0.0.1:8000/wall/21/ - format: http://127.0.0.1:8000/wall/thread_id

It displays nothing. The web console of firefox does not display any errors either.

My view code is the following:

def thread_view(request, wall, Id):
    if request.method == 'GET':
        thread = api.get_thread(wall, Id)
        if thread != None:
            return HttpResponse(thread, content_type="application/json")
        else:
            return HttpResponse("No results")
    else:
        raise Http404



It takes information from the database and formats it into json using a simple serializer, and then sends the data.

What am I missing?

Extra info:

Serializer:

from bson import json_util
class JSONSerializer(object):
    def dumps(self, obj):
        return json_util.dumps(obj, separators=(',', ':')).encode('utf-8')

    def loads(self, data):
        return json_util.loads(data.decode('utf-8'))


I am using Django 1.6, JQuery 1.1, Python 2.7

P.S
When I enter the following url: http://127.0.0.1:8000/wall/21/ firefox displays the JSON object just fine and it's exactly the same as the one in the js file.

解决方案

(document).ready(function(){


("button").click(function(){


.getJSON("testjson.js",function(result){


这篇关于Django JSON响应不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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