Django 使用 HttpResponse 返回 json 字符串显示 Unicode 编码
生活随笔
收集整理的這篇文章主要介紹了
Django 使用 HttpResponse 返回 json 字符串显示 Unicode 编码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
用Django寫Web的時候,服務端接受數據并處理之后想返回給前端一些數據,但是顯示的確實Unicode字符:
{"rep": "\u4f60\u597d\u5440\uff0c\u5f88\u9ad8\u5174\u8ba4\u8bc6\u4f60\u3002", "status": 1}原來后端代碼為:
return HttpResponse(json.dumps({"rep" : rep,"status" : status}))現在改為:
return HttpResponse(json.dumps({"rep" : rep,"status" : status},ensure_ascii=False),content_type="application/json,charset=utf-8")這樣就可以顯示中文了:
{"rep": "你也好呀。", "status": 1}總結
以上是生活随笔為你收集整理的Django 使用 HttpResponse 返回 json 字符串显示 Unicode 编码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 树莓派搭建Django服务器通过远程访问
- 下一篇: Uncaught TypeError: