典型的工作流程是让服务器返回一个JSON对象作为文本,然后interpret that object in the javascript。在你的情况下,你可以从服务器返回文本{“httpresponse”:1},或者使用python json库文件为你生成。
jQuery有一个很好的JSON阅读器(我刚才读的文档,所以有可能是在我的例子中的错误)
的Javascript:
$.getJSON("/abc/?x="+3,
function(data){
if (data["HTTPRESPONSE"] == 1)
{
alert("success")
}
});
Django的
#you might need to easy_install this
import json
def your_view(request):
# You can dump a lot of structured data into a json object, such as
# lists and touples
json_data = json.dumps({"HTTPRESPONSE":1})
# json data is just a JSON string now.
return HttpResponse(json_data, mimetype="application/json")
替代由Issy提供的视图(可爱,因为它遵循DRY原则)
def updates_after_t(request, id):
response = HttpResponse()
response['Content-Type'] = "text/javascript"
response.write(serializers.serialize("json",
TSearch.objects.filter(pk__gt=id)))
return response
有人给了我几个星期前有关这个好回答:http://stackoverflow.com/questions/1457735/django-models-are-not-ajax-serializable – theycallmemorty 2009-10-06 20:45:17
另一种方式返回json数据,从一个模型... def updates_after_t(request,id): response = HttpResponse() response ['Content-Type'] =“text/javascript” response.write(serializers.serialize(“json”, TSearch.objects.filter(pk__gt = id))) 返回响应 – ismail 2009-10-06 23:05:26