Django返回Json
2016-03-18 17:00
447 查看
使用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.6django 1.6
根据百度来的文章,使用 django的simplejson,也被IDE建议使用json。post的回调函数,只需要 :
function(data,status){
if(status == 'success') {
alert(data.box);
}}
使用.号来进行得对应Key值。
前端和后端都指定utf-8编码,python返回中文,直接 {'status':'成功'},连u前缀都不用。
一般使用字典,而不是列表来返回 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.6django 1.6
根据百度来的文章,使用 django的simplejson,也被IDE建议使用json。post的回调函数,只需要 :
function(data,status){
if(status == 'success') {
alert(data.box);
}}
使用.号来进行得对应Key值。
前端和后端都指定utf-8编码,python返回中文,直接 {'status':'成功'},连u前缀都不用。
相关文章推荐
- XML 与 JSON 优劣对比
- VBA将excel数据表生成JSON文件
- newtonsoft.json解析天气数据出错解决方法
- vbs 解析json jsonp的方法
- Firefox返回时Iframe的显示Bug的解决方法
- Extjs4如何处理后台json数据中日期和时间
- C#实现将类的内容写成JSON格式字符串的方法
- JQuery ajax返回JSON时的处理方式 (三种方式)
- jquery JSON的解析方式示例介绍
- c#版json数据解析示例分享
- ASP JSON类文件的使用方法
- C#实现对Json字符串处理实例
- C#实现Json转Unicode的方法
- php中实现可以返回多个值的函数实例
- php解析json数据实例
- PHP中JSON的应用技巧
- PHP6 先修班 JSON实例代码
- 关于JSON以及JSON在PHP中的应用技巧
- .Net中的json操作类用法分析