微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Django 中的JsonResponse 和 json区别

from django.http import JsonResponse
JsonResponse  里面代码会加这一个响应头 kwargs.setdefault('content_type', 'application/json') 告诉浏览器发送的是一个json字符串,不需要在进行 JSON.parse ;
return JsonResponse({"msg":"ok!"})  ----> 返回到ajax里面 这个直接就是一个对象了,不需要在进行 JSON.parse;

注意:
  JsonResponse 如果传递不是一个字典类型,这时候会报  In order to allow non-dict objects to be serialized set the ''safe parameter to False   
  ret = list(models.Comment.objects.filter(article_id=article_id).values("nid","content","parent_comment_id","user__username")) #传递是一个list 
  print(ret)
  return JsonResponse(ret,safe=False) #必须要加safe=False,不然会报'In order to allow non-dict objects to be serialized set the ''safe parameter to False.'


json 需要进行 JSON.parse

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐