Click here to Skip to main content
14,982,215 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

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


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

JavaScript
<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:
Python
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:
Python
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.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900