django处理ajax
用django处理ajax请求,返回JSON数据。
from django.http import HttpResponse
from django.core import serializers
from xxxx import ABC
def abc(request,id):
>> response = HttpResponse()
>> response['Content-Type'] = "text/javascript"
>> response.write(serializers.serialize("json",ABC.objects.filter(pk_gt=id)))
>> return response
注意,如果官户端用get方式发送数据,是不会有任何问题的。但如果官户端用post方式发送数据,一定会返回403的。解决方法如下:
https://docs.djangoproject.com/en/dev/ref/contrib/csrf/
http://blog.donews.com/crazyone/archive/2011/06/23/816685.aspx
http://blog.sina.com.cn/s/blog_634bc2230100q7du.html