使用Django和Python创建Json response的方法
2018-03-26 16:26
966 查看
使用jQuery的.post提交,并期望得到多个数据,Python后台要使用json格式。
不指定datatype为json,让jquery自行判断数据类型。(注:跨域名请求数据,则使用 jsonp字符串)
若post指定数据类型json,则python取post数据,我觉着麻烦。让jquery智能判断,python返回字典最方便。
一般使用字典,而不是列表来返回 JSON内容.
import json from django.http import HttpResponse response_data = {} response_data['result'] = 'failed' response_data['message'] = 'You messed up' return HttpResponse(json.dumps(response_data), content_type="application/json")
for correct - not specifying the mimetype will get you into trouble
正确-不指定mimetype 会导致麻烦
content_type should be used now --mimetype is now deprecated
mimetype 不推荐使用,应当使用content_type 。
不使用content_type,则只能接收第1个字符串。
环境:
python 2.7.6
django 1.6
根据百度来的文章,使用 django的simplejson,也被IDE建议使用json。
post的回调函数,只需要 :
function(data,status){ if(status == 'success') { alert(data.box); }}
使用.号来进行得对应Key值。
前端和后端都指定utf-8编码,python返回中文,直接 {'status':'成功'},连u前缀都不用。
以上这篇使用Django和Python创建Json response的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
- python 接口返回的json字符串实例
- Python实现将json文件中向量写入Excel的方法
- python 对象和json互相转换方法
- 利用python将json数据转换为csv格式的方法
- python如何读写json数据
- python2.7 json 转换日期的处理的示例
- python3.x上post发送json数据
- python使用json序列化datetime类型实例解析
- Python Json序列化与反序列化的示例
- Python实现JSON反序列化类对象的示例
- python 3.6 tkinter+urllib+json实现火车车次信息查询功能
- Python简单读取json文件功能示例
- 深入分析python数据挖掘 Json结构分析
相关文章推荐
- 使用Django和Python创建Json response
- 使用Django和Python创建Json response
- cocos2d-x使用python脚本创建项目的简单方法
- cocos2d-x 使用 python脚本 创建项目的简单方法
- <django中render_to_response的可选参数和使用方法>
- 在 Windows Azure 网站上使用 Django、Python 和 MySQL:创建博客应用程序
- python Django模板的使用方法(图文)
- Python使用multiprocessing创建进程的方法
- cocos2d-x使用python脚本创建项目的简单方法
- cocos2d-x 2.x使用python脚本快捷创建项目的简单方法
- 在Python的Django框架中创建和使用模版
- Django框架中render_to_response()函数的使用方法
- python simplejson模块的使用方法
- springmvc使用@ResponseBody返回json乱码解决方法
- Python使用django获取用户IP地址的方法
- cocos2d-x使用python脚本创建项目的简单方法
- springmvc使用@ResponseBody返回json乱码解决方法
- 在 Windows Azure 网站上使用 Django、Python 和 MySQL:创建博客应用程序
- python Django模板的使用方法(图文)
- Python使用django获取用户IP地址的方法