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.