使用Django和Python创建Json response的方法


Posted in Python onMarch 26, 2018

使用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 相关文章推荐
python实现的解析crontab配置文件代码
Jun 30 Python
Python实现抓取网页并且解析的实例
Sep 20 Python
python每隔N秒运行指定函数的方法
Mar 16 Python
状态机的概念和在Python下使用状态机的教程
Apr 11 Python
使用Python中的线程进行网络编程的入门教程
Apr 15 Python
浅析Python中MySQLdb的事务处理功能
Sep 21 Python
python中多个装饰器的执行顺序详解
Oct 08 Python
python如何通过闭包实现计算器的功能
Feb 22 Python
jupyter notebook 参数传递给shell命令行实例
Apr 10 Python
Python使用Selenium实现淘宝抢单的流程分析
Jun 23 Python
Python利用Pillow(PIL)库实现验证码图片的全过程
Oct 04 Python
python3实现Dijkstra算法最短路径的实现
May 12 Python
python查询mysql,返回json的实例
Mar 26 #Python
Python发送http请求解析返回json的实例
Mar 26 #Python
Flask 让jsonify返回的json串支持中文显示的方法
Mar 26 #Python
详谈在flask中使用jsonify和json.dumps的区别
Mar 26 #Python
python和flask中返回JSON数据的方法
Mar 26 #Python
Python爬虫实战:分析《战狼2》豆瓣影评
Mar 26 #Python
Python简单实现查找一个字符串中最长不重复子串的方法
Mar 26 #Python
You might like
php链表用法实例分析
2015/07/09 PHP
如何正确配置Nginx + PHP
2016/07/15 PHP
PHP实现防盗链的方法分析
2017/07/25 PHP
jquery 页面滚动到指定DIV实现代码
2013/09/25 Javascript
node.js中的buffer.slice方法使用说明
2014/12/10 Javascript
简易的投票系统以及js刷票思路和方法
2015/04/07 Javascript
jquery append 动态添加的元素事件on 不起作用的解决方案
2015/07/30 Javascript
全面解析Bootstrap弹窗的实现方法
2015/12/01 Javascript
完美解决jQuery fancybox ie 无法显示关闭按钮的问题
2016/11/29 Javascript
详解js的事件处理函数和动态创建html标记方法
2016/12/16 Javascript
AngularJS 验证码60秒倒计时功能的实现
2017/06/05 Javascript
js实时监控文本框输入字数的实例代码
2018/01/18 Javascript
Layui 动态禁止select下拉的例子
2019/09/03 Javascript
python实现socket端口重定向示例
2014/02/10 Python
Python中统计函数运行耗时的方法
2015/05/05 Python
python简单读取大文件的方法
2016/07/01 Python
Python基础教程之tcp socket编程详解及简单实例
2017/02/23 Python
关于python2 csv写入空白行的问题
2018/06/22 Python
使用python中的in ,not in来检查元素是不是在列表中的方法
2018/07/06 Python
django 发送邮件和缓存的实现代码
2018/07/18 Python
对Python中实现两个数的值交换的集中方法详解
2019/01/11 Python
详解Python中的各种转义符\n\r\t
2019/07/10 Python
使用遗传算法求二元函数的最小值
2020/02/11 Python
python 爬虫之selenium可视化爬虫的实现
2020/12/04 Python
伦敦一家西班牙童装精品店:La Coqueta
2018/02/02 全球购物
采购员岗位职责
2013/11/15 职场文书
施工资料员岗位职责
2014/01/06 职场文书
学生自我评价范文
2014/02/02 职场文书
小溪流的歌教学反思
2014/02/13 职场文书
理工学院学生自我鉴定
2014/02/23 职场文书
生产文员岗位职责
2014/04/05 职场文书
高考寄语大全
2014/04/08 职场文书
电视节目策划方案
2014/05/16 职场文书
2015公务员试用期工作总结
2014/12/12 职场文书
Python-typing: 类型标注与支持 Any类型详解
2021/05/10 Python
MongoDB数据库之添删改查
2022/04/26 MongoDB